-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
172 lines (140 loc) · 5.06 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************************
Random.cc - description
-------------------
begin : Fri Nov 10 2000
copyright : (C) 2000 by Christian Blum
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "random.h"
#include <math.h>
#include <stdio.h>
#define VERBOSE(x) x
#define VERYVERBOSE(x)
#define PI 3.14159265358979323846264338327
// Any static (but not constant) _class_ variables must be explicitly
/// instantiated once (and only once), so we do it here.
/// Seed the random number generator
long int Random::seed = 123456789;
/* pseudo-random number generator as proposed in numerical recipes in C
Input: a long value; has to be the seed variable
Output: a pseudo-random number uniformly distributed in [0,1]
Side effects: changes the value of the input variable, must be this way
*/
double Random::ran01( long *idum )
{
long k;
double ans;
// printf("seed: %d", *idum);
k =((*idum))/IQ;
*idum = 1 + IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 ) *idum += IM;
ans = AM * (*idum);
// printf(", seed: %d\n", *idum);
return ans;
}
// uniformly distributed double in [lbound, ubound)
double Random::nextDouble(double lbound, double ubound){
return nextDouble() * (ubound - lbound) + lbound;
}
// uniformly distributed double in [0, ubound)
double Random::nextDouble(double ubound){
return nextDouble() * ubound;
}
// uniformly distributed integer in {lbound, ..., ubound-1}
int Random::nextInt(int lbound, int ubound){
// [0, ubound+lbound)
double tmp = nextDouble(ubound - lbound);
// {0, 1, 2, ..., ubound+lbound-1}
int result = (int)tmp;
// {-lbound, ..., ubound-1}
result += lbound;
return result;
}
// uniformly distributed integer in {0, ..., ubound-1}
int Random::nextInt(int ubound){
return nextInt(0, ubound);
}
// N(0,1) distributed double, Box Muller method
double Random::nextNormGaussian(){
double r1 = nextDouble();
double r2 = nextDouble();
double delta1 = sqrt (-2.0 * log(r1)) * cos(2.0 * PI * r2);
//double delta2 = sqrt (-2.0 * log(r1)) * sin(2.0 * PI * r2);
return delta1;
}
// N(mean, sigma2) distributed double
double Random::nextGaussian(double mean, double sigma){
return sigma * nextNormGaussian() + mean;
}
// uniformly distributed boolean
bool Random::nextBoolean(){
return (nextDouble() < 0.5);
}
// Any static (but not constant) _class_ variables must be explicitly
/// instantiated once (and only once), so we do it here.
/// Seed the random number generator
long int Random2::seed = 123456789;
/* pseudo-random number generator as proposed in numerical recipes in C
Input: a long value; has to be the seed variable
Output: a pseudo-random number uniformly distributed in [0,1]
Side effects: changes the value of the input variable, must be this way
*/
double Random2::ran01( long *idum )
{
long k;
double ans;
// printf("seed: %d", *idum);
k =((*idum))/IQ;
*idum = 1 + IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 ) *idum += IM;
ans = AM * (*idum);
// printf(", seed: %d\n", *idum);
return ans;
}
// uniformly distributed double in [lbound, ubound)
double Random2::nextDouble(double lbound, double ubound){
return nextDouble() * (ubound - lbound) + lbound;
}
// uniformly distributed double in [0, ubound)
double Random2::nextDouble(double ubound){
return nextDouble() * ubound;
}
// uniformly distributed integer in {lbound, ..., ubound-1}
int Random2::nextInt(int lbound, int ubound){
// [0, ubound+lbound)
double tmp = nextDouble(ubound - lbound);
// {0, 1, 2, ..., ubound+lbound-1}
int result = (int)tmp;
// {-lbound, ..., ubound-1}
result += lbound;
return result;
}
// uniformly distributed integer in {0, ..., ubound-1}
int Random2::nextInt(int ubound){
return nextInt(0, ubound);
}
// N(0,1) distributed double, Box Muller method
double Random2::nextNormGaussian(){
double r1 = nextDouble();
double r2 = nextDouble();
double delta1 = sqrt (-2.0 * log(r1)) * cos(2.0 * PI * r2);
//double delta2 = sqrt (-2.0 * log(r1)) * sin(2.0 * PI * r2);
return delta1;
}
// N(mean, sigma2) distributed double
double Random2::nextGaussian(double mean, double sigma){
return sigma * nextNormGaussian() + mean;
}
// uniformly distributed boolean
bool Random2::nextBoolean(){
return (nextDouble() < 0.5);
}
// #endif