From be3f19c811347c65acc1415eb06a8384e57fc8d0 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 2 Aug 2013 01:19:23 -0700 Subject: [PATCH] Add Random::fillBitsRandomly --- .../modules/beast_core/maths/beast_Random.cpp | 31 +++++++++---------- .../modules/beast_core/maths/beast_Random.h | 7 ++--- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Subtrees/beast/modules/beast_core/maths/beast_Random.cpp b/Subtrees/beast/modules/beast_core/maths/beast_Random.cpp index 97fdc0a42b..4e8cf36d5c 100644 --- a/Subtrees/beast/modules/beast_core/maths/beast_Random.cpp +++ b/Subtrees/beast/modules/beast_core/maths/beast_Random.cpp @@ -103,23 +103,6 @@ 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; @@ -133,6 +116,20 @@ BigInteger Random::nextLargeNumber (const BigInteger& maximumValue) return n; } +void Random::fillBitsRandomly (void* const buffer, size_t bytes) +{ + int* d = static_cast (buffer); + + for (; bytes >= sizeof (int); bytes -= sizeof (int)) + *d++ = nextInt(); + + if (bytes > 0) + { + const int lastBytes = nextInt(); + memcpy (d, &lastBytes, bytes); + } +} + void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits) { arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space diff --git a/Subtrees/beast/modules/beast_core/maths/beast_Random.h b/Subtrees/beast/modules/beast_core/maths/beast_Random.h index 1e68b1959c..26d77c8a9b 100644 --- a/Subtrees/beast/modules/beast_core/maths/beast_Random.h +++ b/Subtrees/beast/modules/beast_core/maths/beast_Random.h @@ -89,16 +89,15 @@ 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). */ BigInteger nextLargeNumber (const BigInteger& maximumValue); + /** Fills a block of memory with random values. */ + void fillBitsRandomly (void* bufferToFill, size_t sizeInBytes); + /** Sets a range of bits in a BigInteger to random values. */ void fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits);