forked from peter-ch/MultiNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cpp
More file actions
169 lines (147 loc) · 4.2 KB
/
Random.cpp
File metadata and controls
169 lines (147 loc) · 4.2 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
///////////////////////////////////////////////////////////////////////////////////////////
// MultiNEAT - Python/C++ NeuroEvolution of Augmenting Topologies Library
//
// Copyright (C) 2012 Peter Chervenski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see < http://www.gnu.org/licenses/ >.
//
// Contact info:
//
// Peter Chervenski < spookey@abv.bg >
// Shane Ryan < shane.mcdonald.ryan@gmail.com >
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// File: Random.cpp
// Description: Definition for a class dealing with random numbers.
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "Random.h"
#include "Utils.h"
namespace NEAT
{
// Seeds the random number generator with this value
void RNG::Seed(int a_Seed)
{
#ifdef USE_BOOST_RANDOM
gen.seed(a_Seed);
#else
srand(a_Seed);
#endif
}
void RNG::TimeSeed()
{
Seed(time(0));
}
// 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
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::RandFloatClamped()
{
return (RandFloat() - RandFloat());
}
// Returns a random number from a gaussian (normal) distribution in the range of [-1 .. 1]
double RNG::RandGaussClamped()
{
#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
}
}
// namespace NEAT