OpenShot Library | OpenShotAudio  0.2.2
juce_Random.h
1 
2 /** @weakgroup juce_core-maths
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  A random number generator.
33 
34  You can create a Random object and use it to generate a sequence of random numbers.
35 
36  @tags{Core}
37 */
38 class JUCE_API Random final
39 {
40 public:
41  //==============================================================================
42  /** Creates a Random object based on a seed value.
43 
44  For a given seed value, the subsequent numbers generated by this object
45  will be predictable, so a good idea is to set this value based
46  on the time, e.g.
47 
48  new Random (Time::currentTimeMillis())
49  */
50  explicit Random (int64 seedValue) noexcept;
51 
52  /** Creates a Random object using a random seed value.
53  Internally, this calls setSeedRandomly() to randomise the seed.
54  */
55  Random();
56 
57  /** Destructor. */
58  ~Random() noexcept;
59 
60  /** Returns the next random 32 bit integer.
61  @returns a random integer from the full range 0x80000000 to 0x7fffffff
62  */
63  int nextInt() noexcept;
64 
65  /** Returns the next random number, limited to a given range.
66  The maxValue parameter may not be negative, or zero.
67  @returns a random integer between 0 (inclusive) and maxValue (exclusive).
68  */
69  int nextInt (int maxValue) noexcept;
70 
71  /** Returns the next random number, limited to a given range.
72  @returns a random integer between the range start (inclusive) and its end (exclusive).
73  */
74  int nextInt (Range<int> range) noexcept;
75 
76  /** Returns the next 64-bit random number.
77  @returns a random integer from the full range 0x8000000000000000 to 0x7fffffffffffffff
78  */
79  int64 nextInt64() noexcept;
80 
81  /** Returns the next random floating-point number.
82  @returns a random value in the range 0 (inclusive) to 1.0 (exclusive)
83  */
84  float nextFloat() noexcept;
85 
86  /** Returns the next random floating-point number.
87  @returns a random value in the range 0 (inclusive) to 1.0 (exclusive)
88  */
89  double nextDouble() noexcept;
90 
91  /** Returns the next random boolean value. */
92  bool nextBool() noexcept;
93 
94  /** Returns a BigInteger containing a random number.
95  @returns a random value in the range 0 to (maximumValue - 1).
96  */
97  BigInteger nextLargeNumber (const BigInteger& maximumValue);
98 
99  /** Fills a block of memory with random values. */
100  void fillBitsRandomly (void* bufferToFill, size_t sizeInBytes);
101 
102  /** Sets a range of bits in a BigInteger to random values. */
103  void fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits);
104 
105  //==============================================================================
106  /** Resets this Random object to a given seed value. */
107  void setSeed (int64 newSeed) noexcept;
108 
109  /** Returns the RNG's current seed. */
110  int64 getSeed() const noexcept { return seed; }
111 
112  /** Merges this object's seed with another value.
113  This sets the seed to be a value created by combining the current seed and this
114  new value.
115  */
116  void combineSeed (int64 seedValue) noexcept;
117 
118  /** Reseeds this generator using a value generated from various semi-random system
119  properties like the current time, etc.
120 
121  Because this function convolves the time with the last seed value, calling
122  it repeatedly will increase the randomness of the final result.
123  */
124  void setSeedRandomly();
125 
126  /** The overhead of creating a new Random object is fairly small, but if you want to avoid
127  it, you can call this method to get a global shared Random object.
128 
129  It's not thread-safe though, so threads should use their own Random object, otherwise
130  you run the risk of your random numbers becoming.. erm.. randomly corrupted..
131  */
132  static Random& getSystemRandom() noexcept;
133 
134 private:
135  //==============================================================================
136  int64 seed;
137 
138  JUCE_LEAK_DETECTOR (Random)
139 };
140 
141 } // namespace juce
142 
143 /** @}*/
An arbitrarily large integer class.
A random number generator.
Definition: juce_Random.h:39
int64 getSeed() const noexcept
Returns the RNG's current seed.
Definition: juce_Random.h:110
#define JUCE_API
This macro is added to all JUCE public class declarations.