-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ran1.cpp
60 lines (45 loc) · 1.17 KB
/
Ran1.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
#include <iostream>
#include "Ran1.h"
const int m1=259200, ia1=7141, ic1=54773;
const int m2=134456, ia2=8121, ic2=28411;
const int m3=243000, ia3=4561, ic3=51349;
const double rm1 = 1.0/m1;
const double rm2 = 1.0/m2;
Ran1::Ran1(int seed){
setSeed(seed);
}
Ran1::~Ran1(){
}
void Ran1::setSeed(int seed){
if(seed<0){
std::cout << "The chosen seed is smaller than 0" << std::endl;
std::cout << "The seed must be larger than 0" << std::endl;
std::cout << "The default seed (0) will be used" << std::endl;
}
ix1=(ic1+seed) % m1;
ix1=(ia1*ix1+ic1) % m1;
ix2=ix1 % m2;
ix1=(ia1*ix1+ic1) % m1;
ix3=ix1 % m3;
for(int i=0; i<97; i++){
ix1=(ia1*ix1+ic1) % m1;
ix2=(ia2*ix2+ic2) % m2;
R[i]=(ix1+ix2*rm2)*rm1;
}
}
double Ran1::getNumber(){
bool found = false;
double random=0;
while(!found){
ix1=(ia1*ix1+ic1) % m1;
ix2=(ia2*ix2+ic2) % m2;
ix3=(ia3*ix3+ic3) % m3;
int j=(97*ix3)/m3;
if(j<97||j>=0){
random=R[j];
R[j]=(ix1+ix2*rm2)*rm1;
}
if (random > 0 && random < 1 ) found = true;
}
return random;
}