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.

189 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. seed = newSeed;
  32. }
  33. void Random::combineSeed (const int64 seedValue) noexcept
  34. {
  35. seed ^= nextInt64() ^ seedValue;
  36. }
  37. void Random::setSeedRandomly()
  38. {
  39. static int64 globalSeed = 0;
  40. combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
  41. combineSeed (Time::getMillisecondCounter());
  42. combineSeed (Time::getHighResolutionTicks());
  43. combineSeed (Time::getHighResolutionTicksPerSecond());
  44. combineSeed (Time::currentTimeMillis());
  45. globalSeed ^= seed;
  46. }
  47. Random& Random::getSystemRandom() noexcept
  48. {
  49. static Random sysRand;
  50. return sysRand;
  51. }
  52. //==============================================================================
  53. int Random::nextInt() noexcept
  54. {
  55. seed = (int64) (((((uint64) seed) * 0x5deece66dLL) + 11) & 0xffffffffffffLL);
  56. return (int) (seed >> 16);
  57. }
  58. int Random::nextInt (const int maxValue) noexcept
  59. {
  60. jassert (maxValue > 0);
  61. return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
  62. }
  63. int Random::nextInt (Range<int> range) noexcept
  64. {
  65. return range.getStart() + nextInt (range.getLength());
  66. }
  67. int64 Random::nextInt64() noexcept
  68. {
  69. return (int64) ((((uint64) (unsigned int) nextInt()) << 32) | (uint64) (unsigned int) 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()) / (std::numeric_limits<uint32>::max() + 1.0f);
  78. }
  79. double Random::nextDouble() noexcept
  80. {
  81. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
  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 (void* const buffer, size_t bytes)
  94. {
  95. int* d = static_cast<int*> (buffer);
  96. for (; bytes >= sizeof (int); bytes -= sizeof (int))
  97. *d++ = nextInt();
  98. if (bytes > 0)
  99. {
  100. const int lastBytes = nextInt();
  101. memcpy (d, &lastBytes, bytes);
  102. }
  103. }
  104. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  105. {
  106. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  107. while ((startBit & 31) != 0 && numBits > 0)
  108. {
  109. arrayToChange.setBit (startBit++, nextBool());
  110. --numBits;
  111. }
  112. while (numBits >= 32)
  113. {
  114. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  115. startBit += 32;
  116. numBits -= 32;
  117. }
  118. while (--numBits >= 0)
  119. arrayToChange.setBit (startBit + numBits, nextBool());
  120. }
  121. //==============================================================================
  122. #if JUCE_UNIT_TESTS
  123. class RandomTests : public UnitTest
  124. {
  125. public:
  126. RandomTests() : UnitTest ("Random", "Maths") {}
  127. void runTest() override
  128. {
  129. beginTest ("Random");
  130. Random r = getRandom();
  131. for (int i = 2000; --i >= 0;)
  132. {
  133. expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0);
  134. expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f);
  135. expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5);
  136. expect (r.nextInt (1) == 0);
  137. int n = r.nextInt (50) + 1;
  138. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  139. n = r.nextInt (0x7ffffffe) + 1;
  140. expect (r.nextInt (n) >= 0 && r.nextInt (n) < n);
  141. }
  142. }
  143. };
  144. static RandomTests randomTests;
  145. #endif
  146. } // namespace juce