The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_RANDOM_JUCEHEADER__
  19. #define __JUCE_RANDOM_JUCEHEADER__
  20. #include "juce_BigInteger.h"
  21. //==============================================================================
  22. /**
  23. A random number generator.
  24. You can create a Random object and use it to generate a sequence of random numbers.
  25. */
  26. class JUCE_API Random
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a Random object based on a seed value.
  31. For a given seed value, the subsequent numbers generated by this object
  32. will be predictable, so a good idea is to set this value based
  33. on the time, e.g.
  34. new Random (Time::currentTimeMillis())
  35. */
  36. explicit Random (int64 seedValue) noexcept;
  37. /** Creates a Random object using a random seed value.
  38. Internally, this calls setSeedRandomly() to randomise the seed.
  39. */
  40. Random();
  41. /** Destructor. */
  42. ~Random() noexcept;
  43. /** Returns the next random 32 bit integer.
  44. @returns a random integer from the full range 0x80000000 to 0x7fffffff
  45. */
  46. int nextInt() noexcept;
  47. /** Returns the next random number, limited to a given range.
  48. The maxValue parameter may not be negative, or zero.
  49. @returns a random integer between 0 (inclusive) and maxValue (exclusive).
  50. */
  51. int nextInt (int maxValue) noexcept;
  52. /** Returns the next 64-bit random number.
  53. @returns a random integer from the full range 0x8000000000000000 to 0x7fffffffffffffff
  54. */
  55. int64 nextInt64() noexcept;
  56. /** Returns the next random floating-point number.
  57. @returns a random value in the range 0 to 1.0
  58. */
  59. float nextFloat() noexcept;
  60. /** Returns the next random floating-point number.
  61. @returns a random value in the range 0 to 1.0
  62. */
  63. double nextDouble() noexcept;
  64. /** Returns the next random boolean value.
  65. */
  66. bool nextBool() noexcept;
  67. /** Returns a BigInteger containing a random number.
  68. @returns a random value in the range 0 to (maximumValue - 1).
  69. */
  70. BigInteger nextLargeNumber (const BigInteger& maximumValue);
  71. /** Sets a range of bits in a BigInteger to random values. */
  72. void fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits);
  73. //==============================================================================
  74. /** Resets this Random object to a given seed value. */
  75. void setSeed (int64 newSeed) noexcept;
  76. /** Merges this object's seed with another value.
  77. This sets the seed to be a value created by combining the current seed and this
  78. new value.
  79. */
  80. void combineSeed (int64 seedValue) noexcept;
  81. /** Reseeds this generator using a value generated from various semi-random system
  82. properties like the current time, etc.
  83. Because this function convolves the time with the last seed value, calling
  84. it repeatedly will increase the randomness of the final result.
  85. */
  86. void setSeedRandomly();
  87. /** The overhead of creating a new Random object is fairly small, but if you want to avoid
  88. it, you can call this method to get a global shared Random object.
  89. It's not thread-safe though, so threads should use their own Random object, otherwise
  90. you run the risk of your random numbers becoming.. erm.. randomly corrupted..
  91. */
  92. static Random& getSystemRandom() noexcept;
  93. private:
  94. //==============================================================================
  95. int64 seed;
  96. JUCE_LEAK_DETECTOR (Random);
  97. };
  98. #endif // __JUCE_RANDOM_JUCEHEADER__