-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random.hpp
57 lines (43 loc) · 1.32 KB
/
Random.hpp
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
#ifndef RANDOM
#define RANDOM
#include<cmath>
#include<ctime>
/*
contains two functions - one returns ints, other returns doubles
both modify the seed during execution
*/
//returns a scaled pseudorandom I4 between a and b (inclusive)
int i4_uniform_ab ( int a, int b, int &seed ){
if(seed == 0){ seed = time(nullptr);}
int c;
const int i4_huge = 2147483647;
int k;
float r;
int value;
//Guarantee A <= B
if ( b < a ){ c = a; a = b; b = c;}
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 ){ seed = seed + i4_huge;}
r = ( float ) ( seed ) * 4.656612875E-10;
//Scale R to lie between A-0.5 and B+0.5.
r = (1.0 - r) * (( float )a - 0.5) + r * (( float )b + 0.5);
value = round(r);
if ( value < a ){ value = a;}
if ( b < value ){ value = b;}
return value;
}
//returns a scaled pseudorandom R8 between a and b (inclusive)
double r8_uniform_ab ( double a, double b, int &seed ){
if(seed == 0){ seed = time(nullptr);}
int i4_huge = 2147483647;
int k;
double value;
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 ){ seed = seed + i4_huge;}
value = ( double ) ( seed ) * 4.656612875E-10;
value = a + ( b - a ) * value;
return value;
}
#endif