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.

192 lines
5.1KB

  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. Random::Random (const int64 seedValue) noexcept : seed (seedValue)
  24. {
  25. }
  26. Random::Random() : seed (1)
  27. {
  28. setSeedRandomly();
  29. }
  30. Random::~Random() noexcept
  31. {
  32. }
  33. void Random::setSeed (const int64 newSeed) noexcept
  34. {
  35. seed = newSeed;
  36. }
  37. void Random::combineSeed (const int64 seedValue) noexcept
  38. {
  39. seed ^= nextInt64() ^ seedValue;
  40. }
  41. void Random::setSeedRandomly()
  42. {
  43. static int64 globalSeed = 0;
  44. combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
  45. combineSeed (Time::getMillisecondCounter());
  46. combineSeed (Time::getHighResolutionTicks());
  47. combineSeed (Time::getHighResolutionTicksPerSecond());
  48. combineSeed (Time::currentTimeMillis());
  49. globalSeed ^= seed;
  50. }
  51. Random& Random::getSystemRandom() noexcept
  52. {
  53. static Random sysRand;
  54. return sysRand;
  55. }
  56. //==============================================================================
  57. int Random::nextInt() noexcept
  58. {
  59. seed = (seed * 0x5deece66dLL + 11) & 0xffffffffffffLL;
  60. return (int) (seed >> 16);
  61. }
  62. int Random::nextInt (const int maxValue) noexcept
  63. {
  64. jassert (maxValue > 0);
  65. return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
  66. }
  67. int Random::nextInt (Range<int> range) noexcept
  68. {
  69. return range.getStart() + nextInt (range.getLength());
  70. }
  71. int64 Random::nextInt64() noexcept
  72. {
  73. return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
  74. }
  75. bool Random::nextBool() noexcept
  76. {
  77. return (nextInt() & 0x40000000) != 0;
  78. }
  79. float Random::nextFloat() noexcept
  80. {
  81. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0f);
  82. }
  83. double Random::nextDouble() noexcept
  84. {
  85. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
  86. }
  87. BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
  88. {
  89. BigInteger n;
  90. do
  91. {
  92. fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
  93. }
  94. while (n >= maximumValue);
  95. return n;
  96. }
  97. void Random::fillBitsRandomly (void* const buffer, size_t bytes)
  98. {
  99. int* d = static_cast<int*> (buffer);
  100. for (; bytes >= sizeof (int); bytes -= sizeof (int))
  101. *d++ = nextInt();
  102. if (bytes > 0)
  103. {
  104. const int lastBytes = nextInt();
  105. memcpy (d, &lastBytes, bytes);
  106. }
  107. }
  108. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  109. {
  110. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  111. while ((startBit & 31) != 0 && numBits > 0)
  112. {
  113. arrayToChange.setBit (startBit++, nextBool());
  114. --numBits;
  115. }
  116. while (numBits >= 32)
  117. {
  118. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  119. startBit += 32;
  120. numBits -= 32;
  121. }
  122. while (--numBits >= 0)
  123. arrayToChange.setBit (startBit + numBits, nextBool());
  124. }
  125. //==============================================================================
  126. #if JUCE_UNIT_TESTS
  127. class RandomTests : public UnitTest
  128. {
  129. public:
  130. RandomTests() : UnitTest ("Random") {}
  131. void runTest() override
  132. {
  133. beginTest ("Random");
  134. Random r = getRandom();
  135. for (int i = 2000; --i >= 0;)
  136. {
  137. expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
  138. expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
  139. expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
  140. expect (r.nextInt (1) == 0);
  141. int n = r.nextInt (50) + 1;
  142. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  143. n = r.nextInt (0x7ffffffe) + 1;
  144. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  145. }
  146. }
  147. };
  148. static RandomTests randomTests;
  149. #endif