Browse Source

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

tags/2021-05-28
jules 17 years ago
parent
commit
1ab63a4316
8 changed files with 697 additions and 697 deletions
  1. +44
    -50
      juce_amalgamated.cpp
  2. +591
    -585
      juce_amalgamated.h
  3. +38
    -5
      src/juce_core/basics/juce_Random.cpp
  4. +15
    -1
      src/juce_core/basics/juce_Random.h
  5. +2
    -1
      src/juce_core/basics/juce_SystemStats.cpp
  6. +0
    -40
      src/juce_core/containers/juce_BitArray.cpp
  7. +0
    -7
      src/juce_core/containers/juce_BitArray.h
  8. +7
    -8
      src/juce_core/cryptography/juce_Primes.cpp

+ 44
- 50
juce_amalgamated.cpp 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);



+ 591
- 585
juce_amalgamated.h
File diff suppressed because it is too large
View File


+ 38
- 5
src/juce_core/basics/juce_Random.cpp View File

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


+ 15
- 1
src/juce_core/basics/juce_Random.h View File

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


+ 2
- 1
src/juce_core/basics/juce_SystemStats.cpp View File

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


+ 0
- 40
src/juce_core/containers/juce_BitArray.cpp View File

@@ -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()
{


+ 0
- 7
src/juce_core/containers/juce_BitArray.h View File

@@ -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.


+ 7
- 8
src/juce_core/cryptography/juce_Primes.cpp View File

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


Loading…
Cancel
Save