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.

105 lines
2.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "Random.h"
  21. #include "../misc/Time.h"
  22. namespace water {
  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. combineSeed (Time::getMillisecondCounter());
  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. 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. }