2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
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.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
28 A random number generator.
30 You can create a Random object and use it to generate a sequence of random numbers.
34 class JUCE_API Random final
37 //==============================================================================
38 /** Creates a Random object based on a seed value.
40 For a given seed value, the subsequent numbers generated by this object
41 will be predictable, so a good idea is to set this value based
44 new Random (Time::currentTimeMillis())
46 explicit Random (int64 seedValue
) noexcept
;
48 /** Creates a Random object using a random seed value.
49 Internally, this calls setSeedRandomly() to randomise the seed.
53 /** Returns the next random 32 bit integer.
54 @returns a random integer from the full range 0x80000000 to 0x7fffffff
56 int nextInt() noexcept
;
58 /** Returns the next random number, limited to a given range.
59 The maxValue parameter may not be negative, or zero.
60 @returns a random integer between 0 (inclusive) and maxValue (exclusive).
62 int nextInt (int maxValue
) noexcept
;
64 /** Returns the next random number, limited to a given range.
65 @returns a random integer between the range start (inclusive) and its end (exclusive).
67 int nextInt (Range
<int> range
) noexcept
;
69 /** Returns the next 64-bit random number.
70 @returns a random integer from the full range 0x8000000000000000 to 0x7fffffffffffffff
72 int64
nextInt64() noexcept
;
74 /** Returns the next random floating-point number.
75 @returns a random value in the range 0 (inclusive) to 1.0 (exclusive)
77 float nextFloat() noexcept
;
79 /** Returns the next random floating-point number.
80 @returns a random value in the range 0 (inclusive) to 1.0 (exclusive)
82 double nextDouble() noexcept
;
84 /** Returns the next random boolean value. */
85 bool nextBool() noexcept
;
87 /** Returns a BigInteger containing a random number.
88 @returns a random value in the range 0 to (maximumValue - 1).
90 BigInteger
nextLargeNumber (const BigInteger
& maximumValue
);
92 /** Fills a block of memory with random values. */
93 void fillBitsRandomly (void* bufferToFill
, size_t sizeInBytes
);
95 /** Sets a range of bits in a BigInteger to random values. */
96 void fillBitsRandomly (BigInteger
& arrayToChange
, int startBit
, int numBits
);
98 //==============================================================================
99 /** Resets this Random object to a given seed value. */
100 void setSeed (int64 newSeed
) noexcept
;
102 /** Returns the RNG's current seed. */
103 int64
getSeed() const noexcept
{ return seed
; }
105 /** Merges this object's seed with another value.
106 This sets the seed to be a value created by combining the current seed and this
109 void combineSeed (int64 seedValue
) noexcept
;
111 /** Reseeds this generator using a value generated from various semi-random system
112 properties like the current time, etc.
114 Because this function convolves the time with the last seed value, calling
115 it repeatedly will increase the randomness of the final result.
117 void setSeedRandomly();
119 /** The overhead of creating a new Random object is fairly small, but if you want to avoid
120 it, you can call this method to get a global shared Random object.
122 It's not thread-safe though, so threads should use their own Random object, otherwise
123 you run the risk of your random numbers becoming.. erm.. randomly corrupted..
125 static Random
& getSystemRandom() noexcept
;
128 //==============================================================================
131 JUCE_LEAK_DETECTOR (Random
)