juce_Random.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_RANDOM_H_INCLUDED
  22. #define JUCE_RANDOM_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A random number generator.
  26. You can create a Random object and use it to generate a sequence of random numbers.
  27. */
  28. class JUCE_API Random
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a Random object based on a seed value.
  33. For a given seed value, the subsequent numbers generated by this object
  34. will be predictable, so a good idea is to set this value based
  35. on the time, e.g.
  36. new Random (Time::currentTimeMillis())
  37. */
  38. explicit Random (int64 seedValue) noexcept;
  39. /** Creates a Random object using a random seed value.
  40. Internally, this calls setSeedRandomly() to randomise the seed.
  41. */
  42. Random();
  43. /** Destructor. */
  44. ~Random() noexcept;
  45. /** Returns the next random 32 bit integer.
  46. @returns a random integer from the full range 0x80000000 to 0x7fffffff
  47. */
  48. int nextInt() noexcept;
  49. /** Returns the next random number, limited to a given range.
  50. The maxValue parameter may not be negative, or zero.
  51. @returns a random integer between 0 (inclusive) and maxValue (exclusive).
  52. */
  53. int nextInt (int maxValue) noexcept;
  54. /** Returns the next random number, limited to a given range.
  55. @returns a random integer between the range start (inclusive) and its end (exclusive).
  56. */
  57. int nextInt (Range<int> range) noexcept;
  58. /** Returns the next 64-bit random number.
  59. @returns a random integer from the full range 0x8000000000000000 to 0x7fffffffffffffff
  60. */
  61. int64 nextInt64() noexcept;
  62. /** Returns the next random floating-point number.
  63. @returns a random value in the range 0 to 1.0
  64. */
  65. float nextFloat() noexcept;
  66. /** Returns the next random floating-point number.
  67. @returns a random value in the range 0 to 1.0
  68. */
  69. double nextDouble() noexcept;
  70. /** Returns the next random boolean value. */
  71. bool nextBool() noexcept;
  72. /** Returns a BigInteger containing a random number.
  73. @returns a random value in the range 0 to (maximumValue - 1).
  74. */
  75. BigInteger nextLargeNumber (const BigInteger& maximumValue);
  76. /** Fills a block of memory with random values. */
  77. void fillBitsRandomly (void* bufferToFill, size_t sizeInBytes);
  78. /** Sets a range of bits in a BigInteger to random values. */
  79. void fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits);
  80. //==============================================================================
  81. /** Resets this Random object to a given seed value. */
  82. void setSeed (int64 newSeed) noexcept;
  83. /** Returns the RNG's current seed. */
  84. int64 getSeed() const noexcept { return seed; }
  85. /** Merges this object's seed with another value.
  86. This sets the seed to be a value created by combining the current seed and this
  87. new value.
  88. */
  89. void combineSeed (int64 seedValue) noexcept;
  90. /** Reseeds this generator using a value generated from various semi-random system
  91. properties like the current time, etc.
  92. Because this function convolves the time with the last seed value, calling
  93. it repeatedly will increase the randomness of the final result.
  94. */
  95. void setSeedRandomly();
  96. /** The overhead of creating a new Random object is fairly small, but if you want to avoid
  97. it, you can call this method to get a global shared Random object.
  98. It's not thread-safe though, so threads should use their own Random object, otherwise
  99. you run the risk of your random numbers becoming.. erm.. randomly corrupted..
  100. */
  101. static Random& getSystemRandom() noexcept;
  102. private:
  103. //==============================================================================
  104. int64 seed;
  105. JUCE_LEAK_DETECTOR (Random)
  106. };
  107. #endif // JUCE_RANDOM_H_INCLUDED