-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear-congruence-pseudo-random.cpp
More file actions
54 lines (47 loc) · 1013 Bytes
/
linear-congruence-pseudo-random.cpp
File metadata and controls
54 lines (47 loc) · 1013 Bytes
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
#include <iostream>
#include <stdexcept>
/**
*
* Pseudo random generator based on equation:
* Xi+1 = (a*Xi + c) % m;
* m > 0
* a (0, m)
* c (0, m)
* X0 [0, m) seed
*
* */
class LcpRand {
int x;
int a;
int m;
int c;
public:
LcpRand(int seed, int mulitplier, int increment, int modulus) {
if (modulus < 0) {
throw std::invalid_argument("modulus has to be greater than 0");
}
if (mulitplier > modulus || mulitplier <= 0) {
throw std::invalid_argument("multiplier range: (0, m)");
}
if (increment > modulus || increment <= 0) {
throw std::invalid_argument("increment range: (0, m)");
}
if (seed > modulus || seed < 0) {
throw std::invalid_argument("seed range: [0, m)");
}
x = seed;
a = mulitplier;
m = modulus;
c = increment;
}
int rand() {
x = ( (a*x) + c ) % m;
return x;
}
};
int main() {
LcpRand lcpr(5, 3, 3, 7);
for (int i = 0; i < 10; i++) {
std::cout << lcpr.rand() << "\n";
}
}