Random.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * File: Random.cpp
  3. * Project: DUtils library
  4. * Author: Dorian Galvez-Lopez
  5. * Date: April 2010
  6. * Description: manages pseudo-random numbers
  7. * License: see the LICENSE.txt file
  8. *
  9. */
  10. #include "Random.h"
  11. #include "Timestamp.h"
  12. #include <cstdlib>
  13. using namespace std;
  14. bool DUtils::Random::m_already_seeded = false;
  15. void DUtils::Random::SeedRand(){
  16. Timestamp time;
  17. time.setToCurrentTime();
  18. srand((unsigned)time.getFloatTime());
  19. }
  20. void DUtils::Random::SeedRandOnce()
  21. {
  22. if(!m_already_seeded)
  23. {
  24. DUtils::Random::SeedRand();
  25. m_already_seeded = true;
  26. }
  27. }
  28. void DUtils::Random::SeedRand(int seed)
  29. {
  30. srand(seed);
  31. }
  32. void DUtils::Random::SeedRandOnce(int seed)
  33. {
  34. if(!m_already_seeded)
  35. {
  36. DUtils::Random::SeedRand(seed);
  37. m_already_seeded = true;
  38. }
  39. }
  40. int DUtils::Random::RandomInt(int min, int max){
  41. int d = max - min + 1;
  42. return int(((double)rand()/((double)RAND_MAX + 1.0)) * d) + min;
  43. }
  44. // ---------------------------------------------------------------------------
  45. // ---------------------------------------------------------------------------
  46. DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer(int min, int max)
  47. {
  48. if(min <= max)
  49. {
  50. m_min = min;
  51. m_max = max;
  52. }
  53. else
  54. {
  55. m_min = max;
  56. m_max = min;
  57. }
  58. createValues();
  59. }
  60. // ---------------------------------------------------------------------------
  61. DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
  62. (const DUtils::Random::UnrepeatedRandomizer& rnd)
  63. {
  64. *this = rnd;
  65. }
  66. // ---------------------------------------------------------------------------
  67. int DUtils::Random::UnrepeatedRandomizer::get()
  68. {
  69. if(empty()) createValues();
  70. DUtils::Random::SeedRandOnce();
  71. int k = DUtils::Random::RandomInt(0, m_values.size()-1);
  72. int ret = m_values[k];
  73. m_values[k] = m_values.back();
  74. m_values.pop_back();
  75. return ret;
  76. }
  77. // ---------------------------------------------------------------------------
  78. void DUtils::Random::UnrepeatedRandomizer::createValues()
  79. {
  80. int n = m_max - m_min + 1;
  81. m_values.resize(n);
  82. for(int i = 0; i < n; ++i) m_values[i] = m_min + i;
  83. }
  84. // ---------------------------------------------------------------------------
  85. void DUtils::Random::UnrepeatedRandomizer::reset()
  86. {
  87. if((int)m_values.size() != m_max - m_min + 1) createValues();
  88. }
  89. // ---------------------------------------------------------------------------
  90. DUtils::Random::UnrepeatedRandomizer&
  91. DUtils::Random::UnrepeatedRandomizer::operator=
  92. (const DUtils::Random::UnrepeatedRandomizer& rnd)
  93. {
  94. if(this != &rnd)
  95. {
  96. this->m_min = rnd.m_min;
  97. this->m_max = rnd.m_max;
  98. this->m_values = rnd.m_values;
  99. }
  100. return *this;
  101. }
  102. // ---------------------------------------------------------------------------