-
Notifications
You must be signed in to change notification settings - Fork 5
/
myRNG.cpp
45 lines (39 loc) · 952 Bytes
/
myRNG.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
#ifndef _RNG_
#include "myRNG.h"
#define _RNG
#endif
/* this constructor sets all values used in Lehmer's algorithm
* to the provided values setting up an RNG */
myRNG::myRNG( void ) {
x = 1.0f;
t = 0.0f;
a = 48271;
m = (pow(2.0f, 31.0f) - 1);
q = 44488;
r = 3399;
}
/* using Lehmer's algorithm (ensuring no overflow)
* gets a float uniformly distributed between 0 and 1 */
float myRNG::next(){
t = a * fmod(x, q) - r * floor(x / q);
if (t < 0) {
t = t + m;
}
x = t;
return (x / m);
}
/* returns a time spent at cash register
* uniformly distributed between 1 and 2 minutes */
int myRNG::nextPos() {
return floor(diff * next() + low);
}
/* this constructor sets all values used in Lehmer's algorithm
* to the provided values setting up an RNG */
void myRNG::setLimits(float newLow, float newHigh) {
low = newLow;
diff = newHigh - newLow;
}
/* resets x to initial value */
void myRNG::resetSeed(float newseed){
x = newseed;
}