-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcsdefs.cpp
76 lines (61 loc) · 1.84 KB
/
xcsdefs.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
////////////////////////////////////////////////////////////////
// xcsmacros.cpp
// source macros file for xcs based single step problems
// developed by AB Siddique
// School of Engineering and Computer Science
// Victoria University of Wellington
/////////////////////////////////////////////////////////////////
#include "xcsdefs.hpp"
#include "math.h"
namespace xcs
{
//VARS
long m_nSeed = 143907; // a number between 1 and _M-1
const long _M = 2147483647; //ant for the random number generator (modulus of PMMLCG = 2^31 -1).
const long _A = 16807; //ant for the random number generator (default = 16807).
const long _Q = _M/_A; // constant for the random number generator (=_M/_A).
const long _R = _M%_A; // constant for the random number generator (=_M mod _A).
//Functions
// sets a random seed in order to randomize the pseudo random generator
void setSeed(long nSeed)
{
m_nSeed=nSeed;
}
long getSeed()
{
return m_nSeed;
}
// returns a floating-point random number generated according to uniform distribution from [0,1]
double fRand()
{
long hi = m_nSeed / _Q;
long lo = m_nSeed % _Q;
long test = _A*lo - _R*hi;
if (test>0)
m_nSeed = test;
else
m_nSeed = test+_M;
return (double)(m_nSeed)/_M;
}
// returns a random number generated according to uniform distribution from [0,n-1]
int nRand(int nNum)
{
int num = (int)(fRand()*(float)nNum);
while(num == nNum)
num = (int)(fRand()*(float)nNum);
return num;
}
double absValue(double fValue)
{
if(fValue < 0.0)
return -1.0*fValue;
else
return fValue;
}
double fRound( double fVal, int nDecimals )
{
double fMult = (double) pow( 10.0, (double) nDecimals );
double fAux = fVal * fMult;
return ((double) round( fAux )) / fMult;
}
} //~end namespace xcs