forked from electrum/ssb-dbgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnd.c
263 lines (215 loc) · 6.74 KB
/
rnd.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* @(#)rnd.c 2.1.8.2
*
*
* RANDOM.C -- Implements Park & Miller's "Minimum Standard" RNG
*
* (Reference: CACM, Oct 1988, pp 1192-1201)
*
* NextRand: Computes next random integer
* UnifInt: Yields an long uniformly distributed between given bounds
* UnifReal: ields a real uniformly distributed between given bounds
* Exponential: Yields a real exponentially distributed with given mean
*
*/
#include "config.h"
#include <stdio.h>
#include <math.h>
#include "dss.h"
#include "rnd.h"
char *env_config PROTO((char *tag, char *dflt));
void NthElement(long, long *);
void
dss_random(long *tgt, long lower, long upper, long stream)
{
*tgt = UnifInt((long)lower, (long)upper, (long)stream);
Seed[stream].usage += 1;
return;
}
void
row_start(int t) \
{
int i;
for (i=0; i <= MAX_STREAM; i++)
Seed[i].usage = 0 ;
return;
}
void
row_stop(int t) \
{
int i;
/* need to allow for handling the master and detail together */
if (t == ORDER_LINE)
t = ORDER;
if (t == PART_PSUPP)
t = PART;
for (i=0; i <= MAX_STREAM; i++)
if ((Seed[i].table == t) || (Seed[i].table == tdefs[t].child))
{
if (set_seeds && (Seed[i].usage > Seed[i].boundary))
{
// HYRISE: Change format specifier.
fprintf(stderr, "\nSEED CHANGE: seed[%d].usage = %ld\n",
i, Seed[i].usage);
Seed[i].boundary = Seed[i].usage;
}
else
{
NthElement((Seed[i].boundary - Seed[i].usage), &Seed[i].value);
}
}
return;
}
void
dump_seeds(int tbl)
{
int i;
for (i=0; i <= MAX_STREAM; i++)
if (Seed[i].table == tbl)
printf("%d:\t%ld\n", i, Seed[i].value);
return;
}
/******************************************************************
NextRand: Computes next random integer
*******************************************************************/
/*
* long NextRand( long nSeed )
*/
long
NextRand(long nSeed)
/*
* nSeed is the previous random number; the returned value is the
* next random number. The routine generates all numbers in the
* range 1 .. nM-1.
*/
{
/*
* The routine returns (nSeed * nA) mod nM, where nA (the
* multiplier) is 16807, and nM (the modulus) is
* 2147483647 = 2^31 - 1.
*
* nM is prime and nA is a primitive element of the range 1..nM-1.
* This * means that the map nSeed = (nSeed*nA) mod nM, starting
* from any nSeed in 1..nM-1, runs through all elements of 1..nM-1
* before repeating. It never hits 0 or nM.
*
* To compute (nSeed * nA) mod nM without overflow, use the
* following trick. Write nM as nQ * nA + nR, where nQ = nM / nA
* and nR = nM % nA. (For nM = 2147483647 and nA = 16807,
* get nQ = 127773 and nR = 2836.) Write nSeed as nU * nQ + nV,
* where nU = nSeed / nQ and nV = nSeed % nQ. Then we have:
*
* nM = nA * nQ + nR nQ = nM / nA nR < nA < nQ
*
* nSeed = nU * nQ + nV nU = nSeed / nQ nV < nU
*
* Since nA < nQ, we have nA*nQ < nM < nA*nQ + nA < nA*nQ + nQ,
* i.e., nM/nQ = nA. This gives bounds on nU and nV as well:
* nM > nSeed => nM/nQ * >= nSeed/nQ => nA >= nU ( > nV ).
*
* Using ~ to mean "congruent mod nM" this gives:
*
* nA * nSeed ~ nA * (nU*nQ + nV)
*
* ~ nA*nU*nQ + nA*nV
*
* ~ nU * (-nR) + nA*nV (as nA*nQ ~ -nR)
*
* Both products in the last sum can be computed without overflow
* (i.e., both have absolute value < nM) since nU*nR < nA*nQ < nM,
* and nA*nV < nA*nQ < nM. Since the two products have opposite
* sign, their sum lies between -(nM-1) and +(nM-1). If
* non-negative, it is the answer (i.e., it's congruent to
* nA*nSeed and lies between 0 and nM-1). Otherwise adding nM
* yields a number still congruent to nA*nSeed, but now between
* 0 and nM-1, so that's the answer.
*/
long nU, nV;
nU = nSeed / nQ;
nV = nSeed - nQ * nU; /* i.e., nV = nSeed % nQ */
nSeed = nA * nV - nU * nR;
if (nSeed < 0)
nSeed += nM;
return (nSeed);
}
/******************************************************************
UnifInt: Yields an long uniformly distributed between given bounds
*******************************************************************/
/*
* long UnifInt( long nLow, long nHigh, long nStream )
*/
long
UnifInt(long nLow, long nHigh, long nStream)
/*
* Returns an integer uniformly distributed between nLow and nHigh,
* including * the endpoints. nStream is the random number stream.
* Stream 0 is used if nStream is not in the range 0..MAX_STREAM.
*/
{
double dRange;
long nTemp;
if (nStream < 0 || nStream > MAX_STREAM)
nStream = 0;
if (nLow > nHigh)
{
nTemp = nLow;
nLow = nHigh;
nHigh = nTemp;
}
dRange = DOUBLE_CAST (nHigh - nLow + 1);
Seed[nStream].value = NextRand(Seed[nStream].value);
nTemp = (long) (((double) Seed[nStream].value / dM) * (dRange));
return (nLow + nTemp);
}
/******************************************************************
UnifReal: Yields a real uniformly distributed between given bounds
*******************************************************************/
/*
* double UnifReal( double dLow, double dHigh, long nStream )
*/
double
UnifReal(double dLow, double dHigh, long nStream)
/*
* Returns a double uniformly distributed between dLow and dHigh,
* excluding the endpoints. nStream is the random number stream.
* Stream 0 is used if nStream is not in the range 0..MAX_STREAM.
*/
{
double dTemp;
if (nStream < 0 || nStream > MAX_STREAM)
nStream = 0;
if (dLow == dHigh)
return (dLow);
if (dLow > dHigh)
{
dTemp = dLow;
dLow = dHigh;
dHigh = dTemp;
}
Seed[nStream].value = NextRand(Seed[nStream].value);
dTemp = ((double) Seed[nStream].value / dM) * (dHigh - dLow);
return (dLow + dTemp);
}
/******************************************************************%
Exponential: Yields a real exponentially distributed with given mean
*******************************************************************/
/*
* double Exponential( double dMean, long nStream )
*/
double
Exponential(double dMean, long nStream)
/*
* Returns a double uniformly distributed with mean dMean.
* 0.0 is returned iff dMean <= 0.0. nStream is the random number
* stream. Stream 0 is used if nStream is not in the range
* 0..MAX_STREAM.
*/
{
double dTemp;
if (nStream < 0 || nStream > MAX_STREAM)
nStream = 0;
if (dMean <= 0.0)
return (0.0);
Seed[nStream].value = NextRand(Seed[nStream].value);
dTemp = (double) Seed[nStream].value / dM; /* unif between 0..1 */
return (-dMean * log(1.0 - dTemp));
}