RandomGenerator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #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. // STL
  29. #include <limits>
  30. namespace love
  31. {
  32. namespace math
  33. {
  34. class RandomGenerator : public Object
  35. {
  36. public:
  37. union State
  38. {
  39. uint64 b64;
  40. struct
  41. {
  42. uint32 a;
  43. uint32 b;
  44. } b32;
  45. };
  46. RandomGenerator();
  47. virtual ~RandomGenerator() {}
  48. /**
  49. * Set pseudo-random state.
  50. * It's up to the implementation how to use this.
  51. **/
  52. void setState(State state);
  53. /**
  54. * Separately set the low and high parts of the pseudo-random state.
  55. **/
  56. inline void setState(uint32 low, uint32 high)
  57. {
  58. State newstate;
  59. #ifdef LOVE_BIG_ENDIAN
  60. newstate.b32.a = high;
  61. newstate.b32.b = low;
  62. #else
  63. newstate.b32.b = high;
  64. newstate.b32.a = low;
  65. #endif
  66. setState(newstate);
  67. }
  68. inline State getState() const
  69. {
  70. return rng_state;
  71. }
  72. inline void getState(uint32 &low, uint32 &high) const
  73. {
  74. #ifdef LOVE_BIG_ENDIAN
  75. high = rng_state.b32.a;
  76. low = rng_state.b32.b;
  77. #else
  78. high = rng_state.b32.b;
  79. low = rng_state.b32.a;
  80. #endif
  81. }
  82. /**
  83. * Return uniformly distributed pseudo random integer.
  84. *
  85. * @return Pseudo random integer in [0,2^64).
  86. **/
  87. uint64 rand();
  88. /**
  89. * Get uniformly distributed pseudo random number in [0,1).
  90. *
  91. * @return Pseudo random number in [0,1).
  92. **/
  93. inline double random()
  94. {
  95. return double(rand()) / (double(std::numeric_limits<uint64>::max()) + 1.0);
  96. }
  97. /**
  98. * Get uniformly distributed pseudo random number in [0,max).
  99. *
  100. * @return Pseudo random number in [0,max).
  101. **/
  102. inline double random(double max)
  103. {
  104. return random() * max;
  105. }
  106. /**
  107. * Get uniformly distributed pseudo random number in [min, max).
  108. *
  109. * @return Pseudo random number in [min, max).
  110. **/
  111. inline double random(double min, double max)
  112. {
  113. return random() * (max - min) + min;
  114. }
  115. /**
  116. * Get normally distributed pseudo random number.
  117. *
  118. * @param stddev Standard deviation of the distribution.
  119. * @return Normally distributed random number with mean 0 and variance (stddev)².
  120. **/
  121. double randomNormal(double stddev);
  122. private:
  123. State rng_state;
  124. double last_randomnormal;
  125. }; // RandomGenerator
  126. } // math
  127. } // love
  128. #endif // LOVE_MATH_RANDOM_GENERATOR_H