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.

201 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. Random::Random (int64 seedValue) noexcept : seed (seedValue)
  20. {
  21. }
  22. Random::Random() : seed (1)
  23. {
  24. setSeedRandomly();
  25. }
  26. void Random::setSeed (const int64 newSeed) noexcept
  27. {
  28. if (this == &getSystemRandom())
  29. {
  30. // Resetting the system Random risks messing up
  31. // JUCE's internal state. If you need a predictable
  32. // stream of random numbers you should use a local
  33. // Random object.
  34. jassertfalse;
  35. return;
  36. }
  37. seed = newSeed;
  38. }
  39. void Random::combineSeed (const int64 seedValue) noexcept
  40. {
  41. seed ^= nextInt64() ^ seedValue;
  42. }
  43. void Random::setSeedRandomly()
  44. {
  45. static std::atomic<int64> globalSeed { 0 };
  46. combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
  47. combineSeed (Time::getMillisecondCounter());
  48. combineSeed (Time::getHighResolutionTicks());
  49. combineSeed (Time::getHighResolutionTicksPerSecond());
  50. combineSeed (Time::currentTimeMillis());
  51. globalSeed ^= seed;
  52. }
  53. Random& Random::getSystemRandom() noexcept
  54. {
  55. static Random sysRand;
  56. return sysRand;
  57. }
  58. //==============================================================================
  59. int Random::nextInt() noexcept
  60. {
  61. seed = (int64) (((((uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
  62. return (int) (seed >> 16);
  63. }
  64. int Random::nextInt (const int maxValue) noexcept
  65. {
  66. jassert (maxValue > 0);
  67. return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
  68. }
  69. int Random::nextInt (Range<int> range) noexcept
  70. {
  71. return range.getStart() + nextInt (range.getLength());
  72. }
  73. int64 Random::nextInt64() noexcept
  74. {
  75. return (int64) ((((uint64) (unsigned int) nextInt()) << 32) | (uint64) (unsigned int) nextInt());
  76. }
  77. bool Random::nextBool() noexcept
  78. {
  79. return (nextInt() & 0x40000000) != 0;
  80. }
  81. float Random::nextFloat() noexcept
  82. {
  83. auto result = static_cast<float> (static_cast<uint32> (nextInt()))
  84. / (static_cast<float> (std::numeric_limits<uint32>::max()) + 1.0f);
  85. return jmin (result, 1.0f - std::numeric_limits<float>::epsilon());
  86. }
  87. double Random::nextDouble() noexcept
  88. {
  89. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
  90. }
  91. BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
  92. {
  93. BigInteger n;
  94. do
  95. {
  96. fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
  97. }
  98. while (n >= maximumValue);
  99. return n;
  100. }
  101. void Random::fillBitsRandomly (void* const buffer, size_t bytes)
  102. {
  103. int* d = static_cast<int*> (buffer);
  104. for (; bytes >= sizeof (int); bytes -= sizeof (int))
  105. *d++ = nextInt();
  106. if (bytes > 0)
  107. {
  108. const int lastBytes = nextInt();
  109. memcpy (d, &lastBytes, bytes);
  110. }
  111. }
  112. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  113. {
  114. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  115. while ((startBit & 31) != 0 && numBits > 0)
  116. {
  117. arrayToChange.setBit (startBit++, nextBool());
  118. --numBits;
  119. }
  120. while (numBits >= 32)
  121. {
  122. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  123. startBit += 32;
  124. numBits -= 32;
  125. }
  126. while (--numBits >= 0)
  127. arrayToChange.setBit (startBit + numBits, nextBool());
  128. }
  129. //==============================================================================
  130. //==============================================================================
  131. #if JUCE_UNIT_TESTS
  132. class RandomTests final : public UnitTest
  133. {
  134. public:
  135. RandomTests()
  136. : UnitTest ("Random", UnitTestCategories::maths)
  137. {}
  138. void runTest() override
  139. {
  140. beginTest ("Random");
  141. Random r = getRandom();
  142. for (int i = 2000; --i >= 0;)
  143. {
  144. expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
  145. expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
  146. expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
  147. expect (r.nextInt (1) == 0);
  148. int n = r.nextInt (50) + 1;
  149. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  150. n = r.nextInt (0x7ffffffe) + 1;
  151. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  152. }
  153. }
  154. };
  155. static RandomTests randomTests;
  156. #endif
  157. } // namespace juce