37 if (
this == &getSystemRandom())
52 seed ^= nextInt64() ^ seedValue;
76 seed = (
int64) (((((
uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
78 return (
int) (seed >> 16);
84 return (
int) ((((
unsigned int) nextInt()) * (
uint64) maxValue) >> 32);
89 return range.getStart() + nextInt (range.getLength());
99 return (
nextInt() & 0x40000000) != 0;
104 auto result =
static_cast<float> (
static_cast<uint32> (
nextInt()))
122 while (n >= maximumValue);
129 int* d =
static_cast<int*
> (buffer);
131 for (; bytes >=
sizeof (
int); bytes -=
sizeof (
int))
136 const int lastBytes =
nextInt();
137 memcpy (d, &lastBytes, bytes);
143 arrayToChange.
setBit (startBit + numBits - 1,
true);
145 while ((startBit & 31) != 0 && numBits > 0)
151 while (numBits >= 32)
158 while (--numBits >= 0)
167class RandomTests final :
public UnitTest
171 :
UnitTest (
"Random", UnitTestCategories::maths)
174 void runTest()
override
176 beginTest (
"Random");
178 Random r = getRandom();
180 for (
int i = 2000; --i >= 0;)
182 expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
183 expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
184 expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
185 expect (r.nextInt (1) == 0);
187 int n = r.nextInt (50) + 1;
188 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
190 n = r.nextInt (0x7ffffffe) + 1;
191 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
196static RandomTests randomTests;
An arbitrarily large integer class.
int getHighestBit() const noexcept
Returns the index of the highest set bit in the number.
BigInteger & setBitRangeAsInt(int startBit, int numBits, uint32 valueToSet)
Sets a range of bits to an integer value.
BigInteger & setBit(int bitNumber)
Sets a specified bit to 1.
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.
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 currentTimeMillis() noexcept
Returns the current system time.
static int64 getHighResolutionTicksPerSecond() noexcept
Returns the resolution of the high-resolution counter in ticks per second.
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
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.
int pointer_sized_int
A signed integer type that's guaranteed to be large enough to hold a pointer without truncating it.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
unsigned long long uint64
A platform-independent 64-bit unsigned integer type.
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.