41 if (
this == &getSystemRandom())
56 seed ^= nextInt64() ^ seedValue;
61 static int64 globalSeed = 0;
63 combineSeed (globalSeed ^ (int64) (pointer_sized_int)
this);
80 seed = (int64) (((((uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
82 return (
int) (seed >> 16);
87 jassert (maxValue > 0);
88 return (
int) ((((
unsigned int) nextInt()) * (uint64) maxValue) >> 32);
93 return range.getStart() + nextInt (range.getLength());
98 return (int64) ((((uint64) (
unsigned int)
nextInt()) << 32) | (uint64) (
unsigned int)
nextInt());
103 return (
nextInt() & 0x40000000) != 0;
108 return static_cast<uint32
> (
nextInt()) / (std::numeric_limits<uint32>::max() + 1.0f);
113 return static_cast<uint32
> (
nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
124 while (n >= maximumValue);
131 int* d =
static_cast<int*
> (buffer);
133 for (; bytes >=
sizeof (int); bytes -=
sizeof (int))
138 const int lastBytes =
nextInt();
139 memcpy (d, &lastBytes, bytes);
145 arrayToChange.
setBit (startBit + numBits - 1,
true);
147 while ((startBit & 31) != 0 && numBits > 0)
153 while (numBits >= 32)
160 while (--numBits >= 0)
170 RandomTests() :
UnitTest (
"Random",
"Maths") {}
172 void runTest()
override
174 beginTest (
"Random");
176 Random r = getRandom();
178 for (
int i = 2000; --i >= 0;)
180 expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
181 expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
182 expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
183 expect (r.nextInt (1) == 0);
185 int n = r.nextInt (50) + 1;
186 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
188 n = r.nextInt (0x7ffffffe) + 1;
189 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
194static RandomTests randomTests;
An arbitrarily large integer class.
int getHighestBit() const noexcept
Returns the index of the highest set bit in the number.
void setBit(int bitNumber)
Sets a specified bit to 1.
void setBitRangeAsInt(int startBit, int numBits, uint32 valueToSet)
Sets a range of bits to an integer value.
A random number generator.
float nextFloat() noexcept
Returns the next random floating-point number.
Random()
Creates a Random object using a random seed value.
void fillBitsRandomly(void *bufferToFill, size_t sizeInBytes)
Fills a block of memory with random values.
bool nextBool() noexcept
Returns the next random boolean value.
void setSeedRandomly()
Reseeds this generator using a value generated from various semi-random system properties like the cu...
void combineSeed(int64 seedValue) noexcept
Merges this object's seed with another value.
int nextInt() noexcept
Returns the next random 32 bit integer.
~Random() noexcept
Destructor.
int64 nextInt64() noexcept
Returns the next 64-bit random number.
void setSeed(int64 newSeed) noexcept
Resets this Random object to a given seed value.
double nextDouble() noexcept
Returns the next random floating-point number.
BigInteger nextLargeNumber(const BigInteger &maximumValue)
Returns a BigInteger containing a random number.
static Random & getSystemRandom() noexcept
The overhead of creating a new Random object is fairly small, but if you want to avoid it,...
A general-purpose range object, that simply represents any linear range with a start and end point.
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
static int64 currentTimeMillis() noexcept
Returns the current system time.
static int64 getHighResolutionTicksPerSecond() noexcept
Returns the resolution of the high-resolution counter in ticks per second.
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
This is a base class for classes that perform a unit test.