-
Notifications
You must be signed in to change notification settings - Fork 56
/
random.c
executable file
·75 lines (68 loc) · 1.98 KB
/
random.c
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
//**********************************************************
// Pseudo random number generator:
// double random
// void seed (lower_limit, higher_limit)
//**********************************************************
//
// A simple linear congruential random number generator
// (Numerical Recipies chapter 7, 1st ed.) with parameters
// from the table on page 198j.
//
// Uses a linear congruential generator to return a value between
// 0 and 1, then scales and shifts it to fill the desired range. This
// range is set when the random number generator seed is called.
//
// USAGE:
//
// pseudo random sequence is seeded with a range
//
// void seed(lower_limit, higher_limit)
//
// and then subsequent calls to the random number generator generates values
// in the sequence:
//
// double drandom()
//
// History:
// Written by Tim Mattson, 9/2007.
// changed to drandom() to avoid collision with standard libraries, 11/2011
static long MULTIPLIER = 1366;
static long ADDEND = 150889;
static long PMOD = 714025;
long random_last = 0;
double random_low, random_hi;
double drandom()
{
long random_next;
double ret_val;
//
// compute an integer random number from zero to mod
//
random_next = (MULTIPLIER * random_last + ADDEND)% PMOD;
random_last = random_next;
//
// shift into preset range
//
ret_val = ((double)random_next/(double)PMOD)*(random_hi-random_low)+random_low;
return ret_val;
}
//
// set the seed and the range
//
void seed(double low_in, double hi_in)
{
if(low_in < hi_in)
{
random_low = low_in;
random_hi = hi_in;
}
else
{
random_low = hi_in;
random_hi = low_in;
}
random_last = PMOD/ADDEND; // just pick something
}
//**********************************************************
// end of pseudo random generator code.
//**********************************************************