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.

184 lines
4.6KB

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