RandomGenerator.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) 2006-2013 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. // STL
  22. #include <cmath>
  23. namespace love
  24. {
  25. namespace math
  26. {
  27. // 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in
  28. // George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003
  29. RandomGenerator::RandomGenerator()
  30. : last_randomnormal(std::numeric_limits<double>::infinity())
  31. {
  32. // because it is too big for some compilers to handle ... if you know what
  33. // i mean
  34. #ifdef LOVE_BIG_ENDIAN
  35. rng_state.b32.a = 0x0139408D;
  36. rng_state.b32.b = 0xCBBF7A44;
  37. #else
  38. rng_state.b32.b = 0x0139408D;
  39. rng_state.b32.a = 0xCBBF7A44;
  40. #endif
  41. }
  42. void RandomGenerator::setState(RandomGenerator::State state)
  43. {
  44. // 0 xor 0 is still 0, so Xorshift can't generate new numbers.
  45. if (state.b64 == 0)
  46. throw love::Exception("Invalid random state.");
  47. rng_state = state;
  48. }
  49. uint64 RandomGenerator::rand()
  50. {
  51. rng_state.b64 ^= (rng_state.b64 << 13);
  52. rng_state.b64 ^= (rng_state.b64 >> 7);
  53. rng_state.b64 ^= (rng_state.b64 << 17);
  54. return rng_state.b64;
  55. }
  56. // Box–Muller transform
  57. double RandomGenerator::randomNormal(double stddev)
  58. {
  59. // use cached number if possible
  60. if (last_randomnormal != std::numeric_limits<double>::infinity())
  61. {
  62. double r = last_randomnormal;
  63. last_randomnormal = std::numeric_limits<double>::infinity();
  64. return r * stddev;
  65. }
  66. double r = sqrt(-2.0 * log(1. - random()));
  67. double phi = 2.0 * LOVE_M_PI * (1. - random());
  68. last_randomnormal = r * cos(phi);
  69. return r * sin(phi) * stddev;
  70. }
  71. } // math
  72. } // love