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.

141 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. Random::Random (const int64 seedValue) noexcept
  21. : seed (seedValue)
  22. {
  23. }
  24. Random::Random()
  25. : seed (1)
  26. {
  27. setSeedRandomly();
  28. }
  29. Random::~Random() noexcept
  30. {
  31. }
  32. void Random::setSeed (const int64 newSeed) noexcept
  33. {
  34. seed = newSeed;
  35. }
  36. void Random::combineSeed (const int64 seedValue) noexcept
  37. {
  38. seed ^= nextInt64() ^ seedValue;
  39. }
  40. void Random::setSeedRandomly()
  41. {
  42. static int64 globalSeed = 0;
  43. combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
  44. combineSeed (Time::getMillisecondCounter());
  45. combineSeed (Time::getHighResolutionTicks());
  46. combineSeed (Time::getHighResolutionTicksPerSecond());
  47. combineSeed (Time::currentTimeMillis());
  48. globalSeed ^= seed;
  49. }
  50. Random& Random::getSystemRandom() noexcept
  51. {
  52. static Random sysRand;
  53. return sysRand;
  54. }
  55. //==============================================================================
  56. int Random::nextInt() noexcept
  57. {
  58. seed = (seed * literal64bit (0x5deece66d) + 11) & literal64bit (0xffffffffffff);
  59. return (int) (seed >> 16);
  60. }
  61. int Random::nextInt (const int maxValue) noexcept
  62. {
  63. jassert (maxValue > 0);
  64. return (int) ((((unsigned int) nextInt()) * (uint64) maxValue) >> 32);
  65. }
  66. int64 Random::nextInt64() noexcept
  67. {
  68. return (((int64) nextInt()) << 32) | (int64) (uint64) (uint32) nextInt();
  69. }
  70. bool Random::nextBool() noexcept
  71. {
  72. return (nextInt() & 0x80000000) != 0;
  73. }
  74. float Random::nextFloat() noexcept
  75. {
  76. return static_cast <uint32> (nextInt()) / (float) 0xffffffff;
  77. }
  78. double Random::nextDouble() noexcept
  79. {
  80. return static_cast <uint32> (nextInt()) / (double) 0xffffffff;
  81. }
  82. BigInteger Random::nextLargeNumber (const BigInteger& maximumValue)
  83. {
  84. BigInteger n;
  85. do
  86. {
  87. fillBitsRandomly (n, 0, maximumValue.getHighestBit() + 1);
  88. }
  89. while (n >= maximumValue);
  90. return n;
  91. }
  92. void Random::fillBitsRandomly (BigInteger& arrayToChange, int startBit, int numBits)
  93. {
  94. arrayToChange.setBit (startBit + numBits - 1, true); // to force the array to pre-allocate space
  95. while ((startBit & 31) != 0 && numBits > 0)
  96. {
  97. arrayToChange.setBit (startBit++, nextBool());
  98. --numBits;
  99. }
  100. while (numBits >= 32)
  101. {
  102. arrayToChange.setBitRangeAsInt (startBit, 32, (unsigned int) nextInt());
  103. startBit += 32;
  104. numBits -= 32;
  105. }
  106. while (--numBits >= 0)
  107. arrayToChange.setBit (startBit + numBits, nextBool());
  108. }
  109. END_JUCE_NAMESPACE