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.

127 lines
4.6KB

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