From 3e9f20da8fa6193dd80d5adbce84b5ed5f88fbe9 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 18 Jul 2013 09:30:51 +0100 Subject: [PATCH] Added another Random::fillBitsRandomly method. --- modules/juce_core/maths/juce_Random.cpp | 14 ++++++++++++++ modules/juce_core/maths/juce_Random.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/modules/juce_core/maths/juce_Random.cpp b/modules/juce_core/maths/juce_Random.cpp index bb5c71d2b3..2f7874bed9 100644 --- a/modules/juce_core/maths/juce_Random.cpp +++ b/modules/juce_core/maths/juce_Random.cpp @@ -116,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/modules/juce_core/maths/juce_Random.h b/modules/juce_core/maths/juce_Random.h index 2abf721c5c..67d6a59cdc 100644 --- a/modules/juce_core/maths/juce_Random.h +++ b/modules/juce_core/maths/juce_Random.h @@ -100,6 +100,9 @@ public: */ 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);