Random.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File: Random.h
  3. * Project: DUtils library
  4. * Author: Dorian Galvez-Lopez
  5. * Date: April 2010, November 2011
  6. * Description: manages pseudo-random numbers
  7. * License: see the LICENSE.txt file
  8. *
  9. */
  10. #pragma once
  11. #ifndef __D_RANDOM__
  12. #define __D_RANDOM__
  13. #include <cstdlib>
  14. #include <vector>
  15. namespace DUtils {
  16. /// Functions to generate pseudo-random numbers
  17. class Random
  18. {
  19. public:
  20. class UnrepeatedRandomizer;
  21. public:
  22. /**
  23. * Sets the random number seed to the current time
  24. */
  25. static void SeedRand();
  26. /**
  27. * Sets the random number seed to the current time only the first
  28. * time this function is called
  29. */
  30. static void SeedRandOnce();
  31. /**
  32. * Sets the given random number seed
  33. * @param seed
  34. */
  35. static void SeedRand(int seed);
  36. /**
  37. * Sets the given random number seed only the first time this function
  38. * is called
  39. * @param seed
  40. */
  41. static void SeedRandOnce(int seed);
  42. /**
  43. * Returns a random number in the range [0..1]
  44. * @return random T number in [0..1]
  45. */
  46. template <class T>
  47. static T RandomValue(){
  48. return (T)rand()/(T)RAND_MAX;
  49. }
  50. /**
  51. * Returns a random number in the range [min..max]
  52. * @param min
  53. * @param max
  54. * @return random T number in [min..max]
  55. */
  56. template <class T>
  57. static T RandomValue(T min, T max){
  58. return Random::RandomValue<T>() * (max - min) + min;
  59. }
  60. /**
  61. * Returns a random int in the range [min..max]
  62. * @param min
  63. * @param max
  64. * @return random int in [min..max]
  65. */
  66. static int RandomInt(int min, int max);
  67. /**
  68. * Returns a random number from a gaussian distribution
  69. * @param mean
  70. * @param sigma standard deviation
  71. */
  72. template <class T>
  73. static T RandomGaussianValue(T mean, T sigma)
  74. {
  75. // Box-Muller transformation
  76. T x1, x2, w, y1;
  77. do {
  78. x1 = (T)2. * RandomValue<T>() - (T)1.;
  79. x2 = (T)2. * RandomValue<T>() - (T)1.;
  80. w = x1 * x1 + x2 * x2;
  81. } while ( w >= (T)1. || w == (T)0. );
  82. w = sqrt( ((T)-2.0 * log( w ) ) / w );
  83. y1 = x1 * w;
  84. return( mean + y1 * sigma );
  85. }
  86. private:
  87. /// If SeedRandOnce() or SeedRandOnce(int) have already been called
  88. static bool m_already_seeded;
  89. };
  90. // ---------------------------------------------------------------------------
  91. /// Provides pseudo-random numbers with no repetitions
  92. class Random::UnrepeatedRandomizer
  93. {
  94. public:
  95. /**
  96. * Creates a randomizer that returns numbers in the range [min, max]
  97. * @param min
  98. * @param max
  99. */
  100. UnrepeatedRandomizer(int min, int max);
  101. ~UnrepeatedRandomizer(){}
  102. /**
  103. * Copies a randomizer
  104. * @param rnd
  105. */
  106. UnrepeatedRandomizer(const UnrepeatedRandomizer& rnd);
  107. /**
  108. * Copies a randomizer
  109. * @param rnd
  110. */
  111. UnrepeatedRandomizer& operator=(const UnrepeatedRandomizer& rnd);
  112. /**
  113. * Returns a random number not given before. If all the possible values
  114. * were already given, the process starts again
  115. * @return unrepeated random number
  116. */
  117. int get();
  118. /**
  119. * Returns whether all the possible values between min and max were
  120. * already given. If get() is called when empty() is true, the behaviour
  121. * is the same than after creating the randomizer
  122. * @return true iff all the values were returned
  123. */
  124. inline bool empty() const { return m_values.empty(); }
  125. /**
  126. * Returns the number of values still to be returned
  127. * @return amount of values to return
  128. */
  129. inline unsigned int left() const { return m_values.size(); }
  130. /**
  131. * Resets the randomizer as it were just created
  132. */
  133. void reset();
  134. protected:
  135. /**
  136. * Creates the vector with available values
  137. */
  138. void createValues();
  139. protected:
  140. /// Min of range of values
  141. int m_min;
  142. /// Max of range of values
  143. int m_max;
  144. /// Available values
  145. std::vector<int> m_values;
  146. };
  147. }
  148. #endif