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.

205 lines
5.3KB

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