RandomGenerator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Copyright (c) 2006-2014 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. union Seed
  39. {
  40. uint64 b64;
  41. struct
  42. {
  43. #ifdef LOVE_BIG_ENDIAN
  44. uint32 high;
  45. uint32 low;
  46. #else
  47. uint32 low;
  48. uint32 high;
  49. #endif
  50. } b32;
  51. };
  52. RandomGenerator();
  53. virtual ~RandomGenerator() {}
  54. /**
  55. * Return uniformly distributed pseudo random integer.
  56. *
  57. * @return Pseudo random integer in [0,2^64).
  58. **/
  59. uint64 rand();
  60. /**
  61. * Get uniformly distributed pseudo random number in [0,1).
  62. *
  63. * @return Pseudo random number in [0,1).
  64. **/
  65. inline double random()
  66. {
  67. return double(rand()) / (double(std::numeric_limits<uint64>::max()) + 1.0);
  68. }
  69. /**
  70. * Get uniformly distributed pseudo random number in [0,max).
  71. *
  72. * @return Pseudo random number in [0,max).
  73. **/
  74. inline double random(double max)
  75. {
  76. return random() * max;
  77. }
  78. /**
  79. * Get uniformly distributed pseudo random number in [min, max).
  80. *
  81. * @return Pseudo random number in [min, max).
  82. **/
  83. inline double random(double min, double max)
  84. {
  85. return random() * (max - min) + min;
  86. }
  87. /**
  88. * Get normally distributed pseudo random number.
  89. *
  90. * @param stddev Standard deviation of the distribution.
  91. * @return Normally distributed random number with mean 0 and variance (stddev)².
  92. **/
  93. double randomNormal(double stddev);
  94. /**
  95. * Set pseudo-random seed.
  96. * It's up to the implementation how to use this.
  97. **/
  98. void setSeed(Seed seed);
  99. /**
  100. * Get the previously set pseudo-random seed.
  101. **/
  102. Seed getSeed() const;
  103. /**
  104. * Set the internal implementation-dependent state value based on a string.
  105. **/
  106. void setState(const std::string &statestr);
  107. /**
  108. * Get a string representation of the implementation-dependent internal
  109. * state value.
  110. **/
  111. std::string getState() const;
  112. private:
  113. Seed seed;
  114. Seed rng_state;
  115. double last_randomnormal;
  116. }; // RandomGenerator
  117. } // math
  118. } // love
  119. #endif // LOVE_MATH_RANDOM_GENERATOR_H