OpenShot Library | libopenshot-audio 0.2.0
juce_Random.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
27{
28}
29
30Random::Random() : seed (1)
31{
33}
34
38
39void Random::setSeed (const int64 newSeed) noexcept
40{
41 if (this == &getSystemRandom())
42 {
43 // Resetting the system Random risks messing up
44 // JUCE's internal state. If you need a predictable
45 // stream of random numbers you should use a local
46 // Random object.
47 jassertfalse;
48 return;
49 }
50
51 seed = newSeed;
52}
53
54void Random::combineSeed (const int64 seedValue) noexcept
55{
56 seed ^= nextInt64() ^ seedValue;
57}
58
70
76
77//==============================================================================
79{
80 seed = (int64) (((((uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
81
82 return (int) (seed >> 16);
83}
84
85int Random::nextInt (const int maxValue) noexcept
86{
87 jassert (maxValue > 0);
88 return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
89}
90
91int Random::nextInt (Range<int> range) noexcept
92{
93 return range.getStart() + nextInt (range.getLength());
94}
95
97{
98 return (int64) ((((uint64) (unsigned int) nextInt()) << 32) | (uint64) (unsigned int) nextInt());
99}
100
102{
103 return (nextInt() & 0x40000000) != 0;
104}
105
107{
108 return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0f);
109}
110
112{
113 return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
114}
115
117{
119
120 do
121 {
122 fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
123 }
124 while (n >= maximumValue);
125
126 return n;
127}
128
129void Random::fillBitsRandomly (void* const buffer, size_t bytes)
130{
131 int* d = static_cast<int*> (buffer);
132
133 for (; bytes >= sizeof (int); bytes -= sizeof (int))
134 *d++ = nextInt();
135
136 if (bytes > 0)
137 {
138 const int lastBytes = nextInt();
139 memcpy (d, &lastBytes, bytes);
140 }
141}
142
144{
145 arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
146
147 while ((startBit & 31) != 0 && numBits > 0)
148 {
149 arrayToChange.setBit (startBit++, nextBool());
150 --numBits;
151 }
152
153 while (numBits >= 32)
154 {
155 arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
156 startBit += 32;
157 numBits -= 32;
158 }
159
160 while (--numBits >= 0)
162}
163
164//==============================================================================
165#if JUCE_UNIT_TESTS
166
167class RandomTests : public UnitTest
168{
169public:
170 RandomTests() : UnitTest ("Random", "Maths") {}
171
172 void runTest() override
173 {
174 beginTest ("Random");
175
176 Random r = getRandom();
177
178 for (int i = 2000; --i >= 0;)
179 {
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);
184
185 int n = r.nextInt (50) + 1;
186 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
187
188 n = r.nextInt (0x7ffffffe) + 1;
189 expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
190 }
191 }
192};
193
194static RandomTests randomTests;
195
196#endif
197
198} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
Array()=default
Creates an empty array.
An arbitrarily large integer class.
A random number generator.
Definition juce_Random.h:39
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,...
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.