Audio plugin host https://kx.studio/carla
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.

juce_Random.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. Random::Random (const int64 seedValue) noexcept : seed (seedValue)
  24. {
  25. }
  26. Random::Random() : 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. #if 0
  46. combineSeed (Time::getMillisecondCounter());
  47. combineSeed (Time::getHighResolutionTicks());
  48. combineSeed (Time::getHighResolutionTicksPerSecond());
  49. #endif
  50. combineSeed (Time::currentTimeMillis());
  51. globalSeed ^= seed;
  52. }
  53. Random& Random::getSystemRandom() noexcept
  54. {
  55. static Random sysRand;
  56. return sysRand;
  57. }
  58. //==============================================================================
  59. int Random::nextInt() noexcept
  60. {
  61. seed = (seed * 0x5deece66dLL + 11) & 0xffffffffffffLL;
  62. return (int) (seed >> 16);
  63. }
  64. int Random::nextInt (const int maxValue) noexcept
  65. {
  66. jassert (maxValue > 0);
  67. return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
  68. }
  69. #if 0
  70. int Random::nextInt (Range<int> range) noexcept
  71. {
  72. return range.getStart() + nextInt (range.getLength());
  73. }
  74. #endif
  75. int64 Random::nextInt64() noexcept
  76. {
  77. return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
  78. }
  79. bool Random::nextBool() noexcept
  80. {
  81. return (nextInt() & 0x40000000) != 0;
  82. }
  83. float Random::nextFloat() noexcept
  84. {
  85. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0f);
  86. }
  87. double Random::nextDouble() noexcept
  88. {
  89. return static_cast<uint32> (nextInt()) / (std::numeric_limits<uint32>::max() + 1.0);
  90. }
  91. #if 0
  92. BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
  93. {
  94. BigInteger n;
  95. do
  96. {
  97. fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
  98. }
  99. while (n >= maximumValue);
  100. return n;
  101. }
  102. void Random::fillBitsRandomly (void* const buffer, size_t bytes)
  103. {
  104. int* d = static_cast<int*> (buffer);
  105. for (; bytes >= sizeof (int); bytes -= sizeof (int))
  106. *d++ = nextInt();
  107. if (bytes > 0)
  108. {
  109. const int lastBytes = nextInt();
  110. memcpy (d, &lastBytes, bytes);
  111. }
  112. }
  113. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  114. {
  115. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  116. while ((startBit & 31) != 0 && numBits > 0)
  117. {
  118. arrayToChange.setBit (startBit++, nextBool());
  119. --numBits;
  120. }
  121. while (numBits >= 32)
  122. {
  123. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  124. startBit += 32;
  125. numBits -= 32;
  126. }
  127. while (--numBits >= 0)
  128. arrayToChange.setBit (startBit + numBits, nextBool());
  129. }
  130. #endif