-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInversive_Generator.cpp
More file actions
80 lines (69 loc) · 2.1 KB
/
Inversive_Generator.cpp
File metadata and controls
80 lines (69 loc) · 2.1 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
#include "aghInclude.h"
InversiveGenerator::InversiveGenerator(unsigned int _MIN, unsigned int _MAX, unsigned int _SEED, unsigned int _a, unsigned int _b):Generator(_MIN, _MAX, _SEED){
this->setMax(_MAX);
this->setA(_a);
this->setB(_b);
}
InversiveGenerator::~InversiveGenerator(){}
void InversiveGenerator::setA(unsigned int _a){
if(_a < 1 || _a > this->getMax()){
_a = 1;
}
a = _a;
}
void InversiveGenerator::setB(unsigned int _b){
if(_b < 1 || _b > this->getMax()){
_b = 1;
}
b = _b;
}
void InversiveGenerator::setMax(unsigned int _MAX){
if(!isPrime(_MAX)){
_MAX++;
while(!isPrime(_MAX)){
_MAX++;
}
}
this->setBaseMax(_MAX);
if(this->getA() > _MAX){
this->setA(1);
}
if(this->getB() > _MAX){
this->setB(1);
}
}
unsigned int InversiveGenerator::getA(){
return a;
}
unsigned int InversiveGenerator::getB(){
return b;
}
int InversiveGenerator::modInverse(int value, int mod){
value %= mod;
int x=1;
while( x < mod && ((value*x)%mod)!=1 ){
x++;
}
return x;
}
bool InversiveGenerator::isPrime(int x){
bool prime=true;
for(int i=2;i<x;i++){
if(x%i==0){
prime=false;
break;
}
}
return prime;
}
int InversiveGenerator::operator() (){
int randomNumber;
if(this->getSeed() == 0){
randomNumber = this->b;
}else{
randomNumber = (a*modInverse(this->getSeed(), this->getMax() )+b) % getMax();
}
randomNumber = this->getMin() + randomNumber % (this->getMax()-this->getMin()+1);
this->setSeed(randomNumber);
return randomNumber;
}