BitmapToolkit Scol plugin
Toggle main menu visibility
Main Page
Topics
Namespaces
Namespace List
Namespace Members
All
c
p
r
w
x
Functions
Typedefs
Enumerations
Enumerator
c
p
r
x
Classes
Class List
Class Hierarchy
Class Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
~
Variables
_
a
b
c
d
e
f
g
h
i
k
l
m
n
p
r
s
t
u
v
w
x
y
z
Typedefs
Enumerations
Enumerator
a
b
c
d
k
m
n
o
p
r
s
t
z
Related Symbols
Files
File List
File Members
All
_
a
b
c
d
e
f
g
l
m
n
o
p
q
r
s
t
u
v
w
Functions
_
c
d
e
f
g
l
o
p
r
s
t
u
w
Variables
a
b
e
m
n
o
s
w
Typedefs
Macros
_
a
b
d
g
m
n
q
r
s
t
v
src
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
16
bool
DUtils::Random::m_already_seeded =
false
;
17
18
void
DUtils::Random::SeedRand
() {
19
Timestamp
time;
20
time.
setToCurrentTime
();
21
srand((
unsigned
)time.
getFloatTime
());
22
}
18
void
DUtils::Random::SeedRand
() {
…
}
23
24
void
DUtils::Random::SeedRandOnce
()
25
{
26
if
(!m_already_seeded)
27
{
28
DUtils::Random::SeedRand
();
29
m_already_seeded =
true
;
30
}
31
}
24
void
DUtils::Random::SeedRandOnce
() {
…
}
32
33
void
DUtils::Random::SeedRand
(
int
seed)
34
{
35
srand(seed);
36
}
33
void
DUtils::Random::SeedRand
(
int
seed) {
…
}
37
38
void
DUtils::Random::SeedRandOnce
(
int
seed)
39
{
40
if
(!m_already_seeded)
41
{
42
DUtils::Random::SeedRand
(seed);
43
m_already_seeded =
true
;
44
}
45
}
38
void
DUtils::Random::SeedRandOnce
(
int
seed) {
…
}
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
}
47
int
DUtils::Random::RandomInt
(
int
min,
int
max) {
…
}
51
52
// ---------------------------------------------------------------------------
53
// ---------------------------------------------------------------------------
54
55
DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
(
int
min,
int
max)
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
}
55
DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
(
int
min,
int
max) {
…
}
70
71
// ---------------------------------------------------------------------------
72
73
DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
74
(
const
DUtils::Random::UnrepeatedRandomizer
& rnd)
75
{
76
*
this
= rnd;
77
}
73
DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
{
…
}
78
79
// ---------------------------------------------------------------------------
80
81
int
DUtils::Random::UnrepeatedRandomizer::get
()
82
{
83
if
(empty()) createValues();
84
85
DUtils::Random::SeedRandOnce
();
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
}
81
int
DUtils::Random::UnrepeatedRandomizer::get
() {
…
}
94
95
// ---------------------------------------------------------------------------
96
97
void
DUtils::Random::UnrepeatedRandomizer::createValues
()
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
}
97
void
DUtils::Random::UnrepeatedRandomizer::createValues
() {
…
}
104
105
// ---------------------------------------------------------------------------
106
107
void
DUtils::Random::UnrepeatedRandomizer::reset
()
108
{
109
if
((
int
)m_values.size() != m_max - m_min + 1) createValues();
110
}
107
void
DUtils::Random::UnrepeatedRandomizer::reset
() {
…
}
111
112
// ---------------------------------------------------------------------------
113
114
DUtils::Random::UnrepeatedRandomizer
&
115
DUtils::Random::UnrepeatedRandomizer::operator=
116
(
const
DUtils::Random::UnrepeatedRandomizer
& rnd)
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
}
115
DUtils::Random::UnrepeatedRandomizer::operator= {
…
}
126
127
// ---------------------------------------------------------------------------
128
129
Random.h
Timestamp.h
DUtils::Random::UnrepeatedRandomizer
Provides pseudo-random numbers with no repetitions.
Definition
Random.h:113
DUtils::Random::UnrepeatedRandomizer::reset
void reset()
Definition
Random.cpp:107
DUtils::Random::UnrepeatedRandomizer::createValues
void createValues()
Definition
Random.cpp:97
DUtils::Random::UnrepeatedRandomizer::m_min
int m_min
Min of range of values.
Definition
Random.h:172
DUtils::Random::UnrepeatedRandomizer::UnrepeatedRandomizer
UnrepeatedRandomizer(int min, int max)
Definition
Random.cpp:55
DUtils::Random::UnrepeatedRandomizer::get
int get()
Definition
Random.cpp:81
DUtils::Random::SeedRandOnce
static void SeedRandOnce()
Definition
Random.cpp:24
DUtils::Random::SeedRand
static void SeedRand()
Definition
Random.cpp:18
DUtils::Random::RandomInt
static int RandomInt(int min, int max)
Definition
Random.cpp:47
DUtils::Timestamp
Timestamp.
Definition
Timestamp.h:20
DUtils::Timestamp::getFloatTime
double getFloatTime() const
Definition
Timestamp.cpp:94
DUtils::Timestamp::setToCurrentTime
void setToCurrentTime()
Definition
Timestamp.cpp:56
Generated by
1.9.8