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.

107 lines
2.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. #include <limits>
  23. namespace water {
  24. Random::Random (const int64 seedValue) noexcept : seed (seedValue)
  25. {
  26. }
  27. Random::Random() : seed (1)
  28. {
  29. setSeedRandomly();
  30. }
  31. Random::~Random() noexcept
  32. {
  33. }
  34. void Random::setSeed (const int64 newSeed) noexcept
  35. {
  36. seed = newSeed;
  37. }
  38. void Random::combineSeed (const int64 seedValue) noexcept
  39. {
  40. seed ^= nextInt64() ^ seedValue;
  41. }
  42. void Random::setSeedRandomly()
  43. {
  44. static int64 globalSeed = 0;
  45. combineSeed (globalSeed ^ (int64) (pointer_sized_int) this);
  46. combineSeed (Time::getMillisecondCounter());
  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 * 0x5deece66dLL + 11) & 0xffffffffffffLL;
  59. return (int) (seed >> 16);
  60. }
  61. int Random::nextInt (const int maxValue) noexcept
  62. {
  63. wassert (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() & 0x40000000) != 0;
  73. }
  74. float Random::nextFloat() noexcept
  75. {
  76. return static_cast<uint32> (nextInt()) / (static_cast<float>(std::numeric_limits<uint32>::max()) + 1.5f);
  77. }
  78. double Random::nextDouble() noexcept
  79. {
  80. return static_cast<uint32> (nextInt()) / (static_cast<double>(std::numeric_limits<uint32>::max() + 1.5));
  81. }
  82. }