-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
rand.c
113 lines (104 loc) · 2.16 KB
/
rand.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Definition of random number generation routines */
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include "nsga2.h"
# include "rand.h"
// double seed;
double oldrand[55];
int jrand;
/* Get seed number for random and start it up */
void randomize(double seed)
{
int j1;
for(j1=0; j1<=54; j1++)
{
oldrand[j1] = 0.0;
}
jrand=0;
warmup_random (seed);
return;
}
/* Get randomize off and running */
void warmup_random (double seed)
{
int j1, ii;
double new_random, prev_random;
oldrand[54] = seed;
new_random = 0.000000001;
prev_random = seed;
for(j1=1; j1<=54; j1++)
{
ii = (21*j1)%54;
oldrand[ii] = new_random;
new_random = prev_random-new_random;
if(new_random<0.0)
{
new_random += 1.0;
}
prev_random = oldrand[ii];
}
advance_random ();
advance_random ();
advance_random ();
jrand = 0;
return;
}
/* Create next batch of 55 random numbers */
void advance_random ()
{
int j1;
double new_random;
for(j1=0; j1<24; j1++)
{
new_random = oldrand[j1]-oldrand[j1+31];
if(new_random<0.0)
{
new_random = new_random+1.0;
}
oldrand[j1] = new_random;
}
for(j1=24; j1<55; j1++)
{
new_random = oldrand[j1]-oldrand[j1-24];
if(new_random<0.0)
{
new_random = new_random+1.0;
}
oldrand[j1] = new_random;
}
}
/* Fetch a single random number between 0.0 and 1.0 */
double randomperc()
{
jrand++;
if(jrand>=55)
{
jrand = 1;
advance_random();
}
return((double)oldrand[jrand]);
}
/* Fetch a single random integer between low and high including the bounds */
int rnd (int low, int high)
{
int res;
if (low >= high)
{
res = low;
}
else
{
res = low + (randomperc()*(high-low+1));
if (res > high)
{
res = high;
}
}
return (res);
}
/* Fetch a single random real number between low and high including the bounds */
double rndreal (double low, double high)
{
return (low + (high-low)*randomperc());
}