1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-04 03:40:07 +00:00

moved the BitArray::fillBitsRandomly() and BitArray::createRandomNumber() methods across to the Random class.

This commit is contained in:
jules 2008-12-06 11:38:58 +00:00
parent e255b6b1f0
commit 1ab63a4316
8 changed files with 414 additions and 414 deletions

View file

@ -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);