-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
154 lines (134 loc) · 3.23 KB
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "boost/date_time/posix_time/posix_time.hpp"
#define USE_BOOST_RANDOM
#include "random.h"
// Seeds the random number generator with this value
void RNG::Seed(long a_Seed)
{
#ifdef USE_BOOST_RANDOM
gen.seed(a_Seed);
#else
srand(a_Seed);
#endif
}
void RNG::TimeSeed()
{
auto now = boost::posix_time::second_clock::local_time();
Seed(now.time_of_day().total_milliseconds());
}
// Returns randomly either 1 or -1
int RNG::RandPosNeg()
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_int_distribution<> dist(0, 1);
int choice = dist(gen);
#else
int choice = rand() % 2;
#endif
if (choice == 0)
return -1;
else
return 1;
}
// Returns a random integer between X and Y
int RNG::RandInt(int aX, int aY)
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_int_distribution<> dist(aX, aY);
return dist(gen);
#else
if (aX == aY)
{
return aX;
}
if (aX == (aY-1))
{
// for two consecutives, pick either with equal probability
if (RandFloat() < 0.5)
{
return aX;
}
else
{
return aY;
}
}
return aX + (rand() % (aY - aX + 1));
#endif
}
// Returns a random number from a uniform distribution in the range of [0 .. 1]
double RNG::RandFloat()
{
#ifdef USE_BOOST_RANDOM
boost::random::uniform_01<> dist;
return dist(gen);
#else
return (double)(rand() % RAND_MAX) / RAND_MAX;
#endif
}
// Returns a random number from a uniform distribution in the range of [-1 .. 1]
double RNG::RandFloatSigned()
{
return (RandFloat() - RandFloat());
}
// Returns a random number from a gaussian (normal) distribution in the range of [-1 .. 1]
double RNG::RandGaussSigned()
{
#ifdef USE_BOOST_RANDOM
boost::random::normal_distribution<> dist;
double pick = dist(gen);
Clamp(pick, -1, 1);
return pick;
#else
static int t_iset=0;
static double t_gset;
double t_fac,t_rsq,t_v1,t_v2;
if (t_iset==0)
{
do
{
t_v1=2.0*(RandFloat())-1.0;
t_v2=2.0*(RandFloat())-1.0;
t_rsq=t_v1*t_v1+t_v2*t_v2;
}
while (t_rsq>=1.0 || t_rsq==0.0);
t_fac=sqrt(-2.0*log(t_rsq)/t_rsq);
t_gset=t_v1*t_fac;
t_iset=1;
double t_tmp = t_v2*t_fac;
Clamp(t_tmp, -1.0, 1.0);
return t_tmp;
}
else
{
t_iset=0;
double t_tmp = t_gset;
Clamp(t_tmp, -1.0, 1.0);
return t_tmp;
}
#endif
}
int RNG::Roulette(std::vector<double>& a_probs)
{
#ifdef USE_BOOST_RANDOM
boost::random::discrete_distribution<> d_dist(a_probs);
return d_dist(gen);
#else
double t_marble = 0, t_spin = 0, t_total_score = 0;
for(unsigned int i=0; i<a_probs.size(); i++)
{
t_total_score += a_probs[i];
}
t_marble = RandFloat() * t_total_score;
int t_chosen = 0;
t_spin = a_probs[t_chosen];
while(t_spin < t_marble)
{
t_chosen++;
t_spin += a_probs[t_chosen];
}
return t_chosen;
#endif
}