57 static std::atomic<int64> globalSeed { 0 };
59 combineSeed (globalSeed ^ (int64) (pointer_sized_int)
this);
76 seed = (int64) (((((uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
78 return (
int) (seed >> 16);
83 jassert (maxValue > 0);
84 return (
int) ((((
unsigned int)
nextInt()) * (uint64) maxValue) >> 32);
89 return range.getStart() +
nextInt (range.getLength());
94 return (int64) ((((uint64) (
unsigned int)
nextInt()) << 32) | (uint64) (
unsigned int)
nextInt());
99 return (
nextInt() & 0x40000000) != 0;
104 auto result =
static_cast<float> (
static_cast<uint32
> (
nextInt()))
105 / (
static_cast<float> (std::numeric_limits<uint32>::max()) + 1.0f);
106 return jmin (result, 1.0f - std::numeric_limits<float>::epsilon());
111 return static_cast<uint32
> (
nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
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;
int getHighestBit() const noexcept
BigInteger & setBitRangeAsInt(int startBit, int numBits, uint32 valueToSet)
BigInteger & setBit(int bitNumber)
float nextFloat() noexcept
void fillBitsRandomly(void *bufferToFill, size_t sizeInBytes)
void combineSeed(int64 seedValue) noexcept
int64 nextInt64() noexcept
void setSeed(int64 newSeed) noexcept
double nextDouble() noexcept
BigInteger nextLargeNumber(const BigInteger &maximumValue)
Random(int64 seedValue) noexcept
static Random & getSystemRandom() noexcept
static int64 getHighResolutionTicks() noexcept
static int64 currentTimeMillis() noexcept
static int64 getHighResolutionTicksPerSecond() noexcept
static uint32 getMillisecondCounter() noexcept