juce_Random.cpp 5.2 KB

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