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.

177 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. Random::Random (const int64 seedValue) noexcept
  22. : seed (seedValue)
  23. {
  24. }
  25. Random::Random()
  26. : 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. int64 Random::nextInt64() noexcept
  68. {
  69. return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
  70. }
  71. bool Random::nextBool() noexcept
  72. {
  73. return (nextInt() & 0x40000000) != 0;
  74. }
  75. float Random::nextFloat() noexcept
  76. {
  77. return static_cast <uint32> (nextInt()) / (float) 0xffffffff;
  78. }
  79. double Random::nextDouble() noexcept
  80. {
  81. return static_cast <uint32> (nextInt()) / (double) 0xffffffff;
  82. }
  83. BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
  84. {
  85. BigInteger n;
  86. do
  87. {
  88. fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
  89. }
  90. while (n >= maximumValue);
  91. return n;
  92. }
  93. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  94. {
  95. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  96. while ((startBit & 31) != 0 && numBits > 0)
  97. {
  98. arrayToChange.setBit (startBit++, nextBool());
  99. --numBits;
  100. }
  101. while (numBits >= 32)
  102. {
  103. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  104. startBit += 32;
  105. numBits -= 32;
  106. }
  107. while (--numBits >= 0)
  108. arrayToChange.setBit (startBit + numBits, nextBool());
  109. }
  110. //==============================================================================
  111. #if JUCE_UNIT_TESTS
  112. class RandomTests : public UnitTest
  113. {
  114. public:
  115. RandomTests() : UnitTest ("Random") {}
  116. void runTest()
  117. {
  118. beginTest ("Random");
  119. for (int j = 10; --j >= 0;)
  120. {
  121. Random r;
  122. r.setSeedRandomly();
  123. for (int i = 20; --i >= 0;)
  124. {
  125. expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
  126. expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
  127. expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
  128. expect (r.nextInt (1) == 0);
  129. int n = r.nextInt (50) + 1;
  130. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  131. n = r.nextInt (0x7ffffffe) + 1;
  132. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  133. }
  134. }
  135. }
  136. };
  137. static RandomTests randomTests;
  138. #endif