RandomGenerator.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2006-2021 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. #ifndef LOVE_MATH_RANDOM_GENERATOR_H
  21. #define LOVE_MATH_RANDOM_GENERATOR_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/Exception.h"
  25. #include "common/math.h"
  26. #include "common/int.h"
  27. #include "common/Object.h"
  28. // C++
  29. #include <limits>
  30. #include <string>
  31. namespace love
  32. {
  33. namespace math
  34. {
  35. class RandomGenerator : public Object
  36. {
  37. public:
  38. static love::Type type;
  39. union Seed
  40. {
  41. uint64 b64;
  42. struct
  43. {
  44. #ifdef LOVE_BIG_ENDIAN
  45. uint32 high;
  46. uint32 low;
  47. #else
  48. uint32 low;
  49. uint32 high;
  50. #endif
  51. } b32;
  52. };
  53. RandomGenerator();
  54. virtual ~RandomGenerator() {}
  55. /**
  56. * Return uniformly distributed pseudo random integer.
  57. *
  58. * @return Pseudo random integer in [0,2^64).
  59. **/
  60. uint64 rand();
  61. /**
  62. * Get uniformly distributed pseudo random number in [0,1).
  63. *
  64. * @return Pseudo random number in [0,1).
  65. **/
  66. inline double random()
  67. {
  68. uint64 r = rand();
  69. // From http://xoroshiro.di.unimi.it
  70. union { uint64 i; double d; } u;
  71. u.i = ((0x3FFULL) << 52) | (r >> 12);
  72. return u.d - 1.0;
  73. }
  74. /**
  75. * Get uniformly distributed pseudo random number in [0,max).
  76. *
  77. * @return Pseudo random number in [0,max).
  78. **/
  79. inline double random(double max)
  80. {
  81. return random() * max;
  82. }
  83. /**
  84. * Get uniformly distributed pseudo random number in [min, max).
  85. *
  86. * @return Pseudo random number in [min, max).
  87. **/
  88. inline double random(double min, double max)
  89. {
  90. return random() * (max - min) + min;
  91. }
  92. /**
  93. * Get normally distributed pseudo random number.
  94. *
  95. * @param stddev Standard deviation of the distribution.
  96. * @return Normally distributed random number with mean 0 and variance (stddev)².
  97. **/
  98. double randomNormal(double stddev);
  99. /**
  100. * Set pseudo-random seed.
  101. * It's up to the implementation how to use this.
  102. **/
  103. void setSeed(Seed seed);
  104. /**
  105. * Get the previously set pseudo-random seed.
  106. **/
  107. Seed getSeed() const;
  108. /**
  109. * Set the internal implementation-dependent state value based on a string.
  110. **/
  111. void setState(const std::string &statestr);
  112. /**
  113. * Get a string representation of the implementation-dependent internal
  114. * state value.
  115. **/
  116. std::string getState() const;
  117. private:
  118. Seed seed;
  119. Seed rng_state;
  120. double last_randomnormal;
  121. }; // RandomGenerator
  122. } // math
  123. } // love
  124. #endif // LOVE_MATH_RANDOM_GENERATOR_H