Random.cpp
Go to the documentation of this file.
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 
11 #include "Random.h"
12 #include "Timestamp.h"
13 #include <cstdlib>
14 using namespace std;
15 
17 
19  Timestamp time;
20  time.setToCurrentTime();
21  srand((unsigned)time.getFloatTime());
22 }
23 
25 {
26  if(!m_already_seeded)
27  {
29  m_already_seeded = true;
30  }
31 }
32 
34 {
35  srand(seed);
36 }
37 
39 {
40  if(!m_already_seeded)
41  {
43  m_already_seeded = true;
44  }
45 }
46 
47 int DUtils::Random::RandomInt(int min, int max){
48  int d = max - min + 1;
49  return int(((double)rand()/((double)RAND_MAX + 1.0)) * d) + min;
50 }
51 
52 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
54 
56 {
57  if(min <= max)
58  {
59  m_min = min;
60  m_max = max;
61  }
62  else
63  {
64  m_min = max;
65  m_max = min;
66  }
67 
68  createValues();
69 }
70 
71 // ---------------------------------------------------------------------------
72 
75 {
76  *this = rnd;
77 }
78 
79 // ---------------------------------------------------------------------------
80 
82 {
83  if(empty()) createValues();
84 
86 
87  int k = DUtils::Random::RandomInt(0, m_values.size()-1);
88  int ret = m_values[k];
89  m_values[k] = m_values.back();
90  m_values.pop_back();
91 
92  return ret;
93 }
94 
95 // ---------------------------------------------------------------------------
96 
98 {
99  int n = m_max - m_min + 1;
100 
101  m_values.resize(n);
102  for(int i = 0; i < n; ++i) m_values[i] = m_min + i;
103 }
104 
105 // ---------------------------------------------------------------------------
106 
108 {
109  if((int)m_values.size() != m_max - m_min + 1) createValues();
110 }
111 
112 // ---------------------------------------------------------------------------
113 
115 DUtils::Random::UnrepeatedRandomizer::operator=
117 {
118  if(this != &rnd)
119  {
120  this->m_min = rnd.m_min;
121  this->m_max = rnd.m_max;
122  this->m_values = rnd.m_values;
123  }
124  return *this;
125 }
126 
127 // ---------------------------------------------------------------------------
128 
129 
d
static void SeedRandOnce()
Definition: Random.cpp:24
double getFloatTime() const
Definition: Timestamp.cpp:93
UnrepeatedRandomizer(int min, int max)
Definition: Random.cpp:55
static bool m_already_seeded
If SeedRandOnce() or SeedRandOnce(int) have already been called.
Definition: Random.h:105
int m_max
Max of range of values.
Definition: Random.h:174
Timestamp.
Definition: Timestamp.h:19
static void SeedRand()
Definition: Random.cpp:18
static int RandomInt(int min, int max)
Definition: Random.cpp:47
void setToCurrentTime()
Definition: Timestamp.cpp:56
Provides pseudo-random numbers with no repetitions.
Definition: Random.h:112
int m_min
Min of range of values.
Definition: Random.h:172
std::vector< int > m_values
Available values.
Definition: Random.h:177


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47