From 8d881d3b6ff2391921ae63e77b3e09a6f4e1edcb Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 17 Jul 2013 21:11:37 -0700 Subject: [PATCH] Add Random::nextBlob --- modules/beast_core/maths/beast_Random.cpp | 17 +++++++++++++++++ modules/beast_core/maths/beast_Random.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/modules/beast_core/maths/beast_Random.cpp b/modules/beast_core/maths/beast_Random.cpp index 90628f605..ecc5921f6 100644 --- a/modules/beast_core/maths/beast_Random.cpp +++ b/modules/beast_core/maths/beast_Random.cpp @@ -98,6 +98,23 @@ double Random::nextDouble() noexcept return static_cast (nextInt()) / (double) 0xffffffff; } +void Random::nextBlob (void* buffer, size_t bytes) +{ + int const remainder = bytes % sizeof (int64); + + { + int64* dest = static_cast (buffer); + for (int i = bytes / sizeof (int64); i > 0; --i) + *dest++ = nextInt64 (); + buffer = dest; + } + + { + int64 const val = nextInt64 (); + memcpy (buffer, &val, remainder); + } +} + BigInteger Random::nextLargeNumber (const BigInteger& maximumValue) { BigInteger n; diff --git a/modules/beast_core/maths/beast_Random.h b/modules/beast_core/maths/beast_Random.h index f35c0eed9..1e68b1959 100644 --- a/modules/beast_core/maths/beast_Random.h +++ b/modules/beast_core/maths/beast_Random.h @@ -89,6 +89,10 @@ public: */ bool nextBool() noexcept; + /** Fills a piece of memory with random data. + */ + void nextBlob (void* buffer, size_t bytes); + /** Returns a BigInteger containing a random number. @returns a random value in the range 0 to (maximumValue - 1).