RandomGenerator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "RandomGenerator.h"
  21. // C++
  22. #include <sstream>
  23. #include <iomanip>
  24. // C
  25. #include <cmath>
  26. #include <cstdlib>
  27. namespace love
  28. {
  29. namespace math
  30. {
  31. // Thomas Wang's 64-bit integer hashing function:
  32. // https://web.archive.org/web/20110807030012/http://www.cris.com/%7ETtwang/tech/inthash.htm
  33. static uint64 wangHash64(uint64 key)
  34. {
  35. key = (~key) + (key << 21); // key = (key << 21) - key - 1;
  36. key = key ^ (key >> 24);
  37. key = (key + (key << 3)) + (key << 8); // key * 265
  38. key = key ^ (key >> 14);
  39. key = (key + (key << 2)) + (key << 4); // key * 21
  40. key = key ^ (key >> 28);
  41. key = key + (key << 31);
  42. return key;
  43. }
  44. // 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in
  45. // George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003
  46. RandomGenerator::RandomGenerator()
  47. : last_randomnormal(std::numeric_limits<double>::infinity())
  48. {
  49. // because it is too big for some compilers to handle ... if you know what
  50. // i mean
  51. Seed newseed;
  52. newseed.b32.low = 0xCBBF7A44;
  53. newseed.b32.high = 0x0139408D;
  54. setSeed(newseed);
  55. }
  56. uint64 RandomGenerator::rand()
  57. {
  58. rng_state.b64 ^= (rng_state.b64 << 13);
  59. rng_state.b64 ^= (rng_state.b64 >> 7);
  60. rng_state.b64 ^= (rng_state.b64 << 17);
  61. return rng_state.b64;
  62. }
  63. // Box–Muller transform
  64. double RandomGenerator::randomNormal(double stddev)
  65. {
  66. // use cached number if possible
  67. if (last_randomnormal != std::numeric_limits<double>::infinity())
  68. {
  69. double r = last_randomnormal;
  70. last_randomnormal = std::numeric_limits<double>::infinity();
  71. return r * stddev;
  72. }
  73. double r = sqrt(-2.0 * log(1. - random()));
  74. double phi = 2.0 * LOVE_M_PI * (1. - random());
  75. last_randomnormal = r * cos(phi);
  76. return r * sin(phi) * stddev;
  77. }
  78. void RandomGenerator::setSeed(RandomGenerator::Seed newseed)
  79. {
  80. seed = newseed;
  81. // Xorshift isn't designed to give a good distribution of values across many
  82. // similar seeds, so we hash the state integer before using it.
  83. // http://www.reedbeta.com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/
  84. // Xorshift also can't handle a state value of 0, so we avoid that.
  85. do
  86. {
  87. newseed.b64 = wangHash64(newseed.b64);
  88. } while (newseed.b64 == 0);
  89. rng_state = newseed;
  90. }
  91. RandomGenerator::Seed RandomGenerator::getSeed() const
  92. {
  93. return seed;
  94. }
  95. void RandomGenerator::setState(const std::string &statestr)
  96. {
  97. // For this implementation we'll accept a hex string representing the
  98. // 64-bit state integer xorshift uses.
  99. // Hex string must start with 0x.
  100. if (statestr.find("0x") != 0 || statestr.size() < 3)
  101. throw love::Exception("Invalid random state: %s", statestr.c_str());
  102. Seed state = {};
  103. char *end = nullptr;
  104. state.b64 = strtoull(statestr.c_str(), &end, 16);
  105. if (end != nullptr && *end != 0)
  106. throw love::Exception("Invalid random state: %s", statestr.c_str());
  107. rng_state = state;
  108. }
  109. std::string RandomGenerator::getState() const
  110. {
  111. // For this implementation we'll return a hex string representing the 64-bit
  112. // state integer xorshift uses.
  113. std::stringstream ss;
  114. ss << "0x" << std::setfill('0') << std::setw(16) << std::hex << rng_state.b64;
  115. return ss.str();
  116. }
  117. } // math
  118. } // love