Audio plugin host https://kx.studio/carla
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.

juce_Random.h 5.5KB

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