| @@ -804,7 +804,7 @@ void Random::setSeedRandomly() | |||
| Random r3 (Time::getHighResolutionTicksPerSecond()); | |||
| Random r4 (Time::currentTimeMillis()); | |||
| setSeed (r1.nextInt64() ^ r2.nextInt64() | |||
| setSeed (nextInt64() ^ r1.nextInt64() ^ r2.nextInt64() | |||
| ^ r3.nextInt64() ^ r4.nextInt64()); | |||
| } | |||
| @@ -841,10 +841,43 @@ double Random::nextDouble() throw() | |||
| return ((uint32) nextInt()) / (double) 0xffffffff; | |||
| } | |||
| static Random sysRand (1); | |||
| const BitArray Random::nextLargeNumber (const BitArray& maximumValue) throw() | |||
| { | |||
| BitArray n; | |||
| do | |||
| { | |||
| fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1); | |||
| } | |||
| while (n.compare (maximumValue) >= 0); | |||
| return n; | |||
| } | |||
| void Random::fillBitsRandomly (BitArray& arrayToChange, int startBit, int numBits) throw() | |||
| { | |||
| arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space | |||
| while ((startBit & 31) != 0 && numBits > 0) | |||
| { | |||
| arrayToChange.setBit (startBit++, nextBool()); | |||
| --numBits; | |||
| } | |||
| while (numBits >= 32) | |||
| { | |||
| arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt()); | |||
| startBit += 32; | |||
| numBits -= 32; | |||
| } | |||
| while (--numBits >= 0) | |||
| arrayToChange.setBit (startBit + numBits, nextBool()); | |||
| } | |||
| Random& Random::getSystemRandom() throw() | |||
| { | |||
| static Random sysRand (1); | |||
| return sysRand; | |||
| } | |||
| @@ -1123,9 +1156,10 @@ void JUCE_PUBLIC_FUNCTION initialiseJuce_NonGUI() | |||
| juceInitialisedNonGUI = true; | |||
| DBG (SystemStats::getJUCEVersion()); | |||
| Random::getSystemRandom().setSeedRandomly(); // (calling this more than once improves its randomness) | |||
| juce_initialiseStrings(); | |||
| SystemStats::initialiseStats(); | |||
| Random::getSystemRandom().setSeedRandomly(); | |||
| Random::getSystemRandom().setSeedRandomly(); // (calling this more than once improves its randomness) | |||
| } | |||
| } | |||
| @@ -2395,45 +2429,6 @@ void BitArray::setBitRangeAsInt (const int startBit, int numBits, unsigned int v | |||
| } | |||
| } | |||
| void BitArray::fillBitsRandomly (int startBit, int numBits) throw() | |||
| { | |||
| highestBit = jmax (highestBit, startBit + numBits); | |||
| ensureSize (((startBit + numBits) >> 5) + 1); | |||
| while ((startBit & 31) != 0 && numBits > 0) | |||
| { | |||
| setBit (startBit++, Random::getSystemRandom().nextBool()); | |||
| --numBits; | |||
| } | |||
| while (numBits >= 32) | |||
| { | |||
| values [startBit >> 5] = (unsigned int) Random::getSystemRandom().nextInt(); | |||
| startBit += 32; | |||
| numBits -= 32; | |||
| } | |||
| while (--numBits >= 0) | |||
| { | |||
| setBit (startBit + numBits, Random::getSystemRandom().nextBool()); | |||
| } | |||
| highestBit = getHighestBit(); | |||
| } | |||
| void BitArray::createRandomNumber (const BitArray& maximumValue) throw() | |||
| { | |||
| clear(); | |||
| do | |||
| { | |||
| fillBitsRandomly (0, maximumValue.getHighestBit() + 1); | |||
| } | |||
| while (compare (maximumValue) >= 0); | |||
| } | |||
| bool BitArray::isNegative() const throw() | |||
| { | |||
| return negative && ! isEmpty(); | |||
| @@ -4076,16 +4071,16 @@ const BitArray Primes::createProbablePrime (const int bitLength, | |||
| const int* randomSeeds, | |||
| int numRandomSeeds) throw() | |||
| { | |||
| int defaultSeeds[8]; | |||
| int defaultSeeds [16]; | |||
| if (numRandomSeeds <= 0) | |||
| { | |||
| randomSeeds = defaultSeeds; | |||
| numRandomSeeds = 8; | |||
| numRandomSeeds = numElementsInArray (defaultSeeds); | |||
| Random r (0); | |||
| for (int j = 10; --j >= 0;) | |||
| { | |||
| Random r (0); | |||
| r.setSeedRandomly(); | |||
| for (int i = numRandomSeeds; --i >= 0;) | |||
| @@ -4101,15 +4096,14 @@ const BitArray Primes::createProbablePrime (const int bitLength, | |||
| for (int i = numRandomSeeds; --i >= 0;) | |||
| { | |||
| Random::getSystemRandom().setSeed (randomSeeds[i]); | |||
| BitArray p2; | |||
| p2.fillBitsRandomly (0, bitLength); | |||
| Random r (randomSeeds[i]); | |||
| r.fillBitsRandomly (p2, 0, bitLength); | |||
| p.xorWith (p2); | |||
| } | |||
| Random::getSystemRandom().setSeedRandomly(); | |||
| p.setBit (bitLength - 1); | |||
| p.clearBit (0); | |||
| @@ -34,8 +34,8 @@ | |||
| BEGIN_JUCE_NAMESPACE | |||
| #include "juce_Random.h" | |||
| #include "../basics/juce_Time.h" | |||
| #include "../basics/juce_SystemStats.h" | |||
| #include "juce_Time.h" | |||
| #include "juce_SystemStats.h" | |||
| //============================================================================== | |||
| @@ -60,7 +60,7 @@ void Random::setSeedRandomly() | |||
| Random r3 (Time::getHighResolutionTicksPerSecond()); | |||
| Random r4 (Time::currentTimeMillis()); | |||
| setSeed (r1.nextInt64() ^ r2.nextInt64() | |||
| setSeed (nextInt64() ^ r1.nextInt64() ^ r2.nextInt64() | |||
| ^ r3.nextInt64() ^ r4.nextInt64()); | |||
| } | |||
| @@ -98,11 +98,44 @@ double Random::nextDouble() throw() | |||
| return ((uint32) nextInt()) / (double) 0xffffffff; | |||
| } | |||
| //============================================================================== | |||
| static Random sysRand (1); | |||
| const BitArray Random::nextLargeNumber (const BitArray& maximumValue) throw() | |||
| { | |||
| BitArray n; | |||
| do | |||
| { | |||
| fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1); | |||
| } | |||
| while (n.compare (maximumValue) >= 0); | |||
| return n; | |||
| } | |||
| void Random::fillBitsRandomly (BitArray& arrayToChange, int startBit, int numBits) throw() | |||
| { | |||
| arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space | |||
| while ((startBit & 31) != 0 && numBits > 0) | |||
| { | |||
| arrayToChange.setBit (startBit++, nextBool()); | |||
| --numBits; | |||
| } | |||
| while (numBits >= 32) | |||
| { | |||
| arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt()); | |||
| startBit += 32; | |||
| numBits -= 32; | |||
| } | |||
| while (--numBits >= 0) | |||
| arrayToChange.setBit (startBit + numBits, nextBool()); | |||
| } | |||
| //============================================================================== | |||
| Random& Random::getSystemRandom() throw() | |||
| { | |||
| static Random sysRand (1); | |||
| return sysRand; | |||
| } | |||
| @@ -32,6 +32,8 @@ | |||
| #ifndef __JUCE_RANDOM_JUCEHEADER__ | |||
| #define __JUCE_RANDOM_JUCEHEADER__ | |||
| #include "../containers/juce_BitArray.h" | |||
| //============================================================================== | |||
| /** | |||
| @@ -88,6 +90,15 @@ public: | |||
| */ | |||
| bool nextBool() throw(); | |||
| /** Returns a BitArray containing a random number. | |||
| @returns a random value in the range 0 to (maximumValue - 1). | |||
| */ | |||
| const BitArray nextLargeNumber (const BitArray& maximumValue) throw(); | |||
| /** Sets a range of bits in a BitArray to random values. */ | |||
| void fillBitsRandomly (BitArray& arrayToChange, int startBit, int numBits) throw(); | |||
| //============================================================================== | |||
| /** To avoid the overhead of having to create a new Random object whenever | |||
| you need a number, this is a shared application-wide object that | |||
| @@ -100,8 +111,11 @@ public: | |||
| /** Resets this Random object to a given seed value. */ | |||
| void setSeed (const int64 newSeed) throw(); | |||
| /** Reseeds this generator using a value generated from various semi-random system | |||
| /** Reseeds this generator using a value generated from various semi-random system | |||
| properties like the current time, etc. | |||
| Because this function convolves the time with the last seed value, calling | |||
| it repeatedly will increase the randomness of the final result. | |||
| */ | |||
| void setSeedRandomly(); | |||
| @@ -84,9 +84,10 @@ void JUCE_PUBLIC_FUNCTION initialiseJuce_NonGUI() | |||
| juceInitialisedNonGUI = true; | |||
| DBG (SystemStats::getJUCEVersion()); | |||
| Random::getSystemRandom().setSeedRandomly(); // (calling this more than once improves its randomness) | |||
| juce_initialiseStrings(); | |||
| SystemStats::initialiseStats(); | |||
| Random::getSystemRandom().setSeedRandomly(); | |||
| Random::getSystemRandom().setSeedRandomly(); // (calling this more than once improves its randomness) | |||
| } | |||
| } | |||
| @@ -736,46 +736,6 @@ void BitArray::setBitRangeAsInt (const int startBit, int numBits, unsigned int v | |||
| } | |||
| } | |||
| //============================================================================== | |||
| void BitArray::fillBitsRandomly (int startBit, int numBits) throw() | |||
| { | |||
| highestBit = jmax (highestBit, startBit + numBits); | |||
| ensureSize (((startBit + numBits) >> 5) + 1); | |||
| while ((startBit & 31) != 0 && numBits > 0) | |||
| { | |||
| setBit (startBit++, Random::getSystemRandom().nextBool()); | |||
| --numBits; | |||
| } | |||
| while (numBits >= 32) | |||
| { | |||
| values [startBit >> 5] = (unsigned int) Random::getSystemRandom().nextInt(); | |||
| startBit += 32; | |||
| numBits -= 32; | |||
| } | |||
| while (--numBits >= 0) | |||
| { | |||
| setBit (startBit + numBits, Random::getSystemRandom().nextBool()); | |||
| } | |||
| highestBit = getHighestBit(); | |||
| } | |||
| void BitArray::createRandomNumber (const BitArray& maximumValue) throw() | |||
| { | |||
| clear(); | |||
| do | |||
| { | |||
| fillBitsRandomly (0, maximumValue.getHighestBit() + 1); | |||
| } | |||
| while (compare (maximumValue) >= 0); | |||
| } | |||
| //============================================================================== | |||
| bool BitArray::isNegative() const throw() | |||
| { | |||
| @@ -289,13 +289,6 @@ public: | |||
| */ | |||
| int getHighestBit() const throw(); | |||
| //============================================================================== | |||
| /** Sets a range of bits to random values. */ | |||
| void fillBitsRandomly (int startBit, int numBits) throw(); | |||
| /** Turns this value into a random number less than the given value. */ | |||
| void createRandomNumber (const BitArray& maximumValue) throw(); | |||
| //============================================================================== | |||
| /** Converts the array to a number string. | |||
| @@ -126,16 +126,16 @@ const BitArray Primes::createProbablePrime (const int bitLength, | |||
| const int* randomSeeds, | |||
| int numRandomSeeds) throw() | |||
| { | |||
| int defaultSeeds[8]; | |||
| int defaultSeeds [16]; | |||
| if (numRandomSeeds <= 0) | |||
| { | |||
| randomSeeds = defaultSeeds; | |||
| numRandomSeeds = 8; | |||
| numRandomSeeds = numElementsInArray (defaultSeeds); | |||
| Random r (0); | |||
| for (int j = 10; --j >= 0;) | |||
| { | |||
| Random r (0); | |||
| r.setSeedRandomly(); | |||
| for (int i = numRandomSeeds; --i >= 0;) | |||
| @@ -151,15 +151,14 @@ const BitArray Primes::createProbablePrime (const int bitLength, | |||
| for (int i = numRandomSeeds; --i >= 0;) | |||
| { | |||
| Random::getSystemRandom().setSeed (randomSeeds[i]); | |||
| BitArray p2; | |||
| p2.fillBitsRandomly (0, bitLength); | |||
| Random r (randomSeeds[i]); | |||
| r.fillBitsRandomly (p2, 0, bitLength); | |||
| p.xorWith (p2); | |||
| } | |||
| Random::getSystemRandom().setSeedRandomly(); | |||
| p.setBit (bitLength - 1); | |||
| p.clearBit (0); | |||