-
Notifications
You must be signed in to change notification settings - Fork 13
/
rnd.c
513 lines (438 loc) · 11.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Sccsid: @(#)rnd.c 9.1.1.9 9/7/95 16:24:04
*
* 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));
#define PI (3.141597)
/******************************************************************
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)
return (nLow);
if (nLow > nHigh)
{
nTemp = nLow;
nLow = nHigh;
nHigh = nTemp;
}
dRange = (double) (nHigh - nLow + 1);
Seed[nStream] = NextRand(Seed[nStream]);
nTemp = (long) (((double) Seed[nStream] / 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] = NextRand(Seed[nStream]);
dTemp = ((double) Seed[nStream] / 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] = NextRand(Seed[nStream]);
dTemp = (double) Seed[nStream] / dM; /* unif between 0..1 */
return (-dMean * log(1.0 - dTemp));
}
/*
* Extensions to dbgen for generation of skewed data.
* Surajit Chaudhuri, Vivek Narasayya.
* (Jan '99)
*/
#define EPSILON (0.0001)
/*
* Round the number x to the nearest integer.
*/
double
round(double x)
{
double fx = floor(x);
double cx = ceil(x);
if((x-fx) <= (cx-x))
return fx;
else
return cx;
}
/*
* Perform binary search to find D (number of distinct values for given n, zipf)
*
*/
double
SolveForMultipler(long n, double zipf)
{
long lowD = 1;
long highD = n;
long mid;
double sumMult;
double nRowsPrime;
long numRows;
long i;
while ( (highD - lowD) > 1)
{
mid = (highD + lowD) / 2;
sumMult = 0.0;
/* compute multiplier */
for(i=1;i<=mid;i++)
{
sumMult += 1.0 / (pow((double) i, zipf));
}
/* compute number of rows generated for this mulitpler */
numRows = 0;
for(i=1;i<=mid;i++)
{
nRowsPrime = ((n / sumMult) / pow((double) i, zipf));
numRows += (long) round(nRowsPrime);
}
if(((double)(n-numRows))/n < EPSILON)
{
break;
}
if(numRows > n)
{
/* we overestimated numRows -- we need fewer distinct values */
highD = mid;
}
else
{
/* underestimation of numRows -- need lower multiplier */
lowD = mid;
}
}
return sumMult;
}
double
GetMultiplier(long n, double zipf)
{
double multiplier;
double term;
long i;
if(zipf == 0.0)
multiplier = n;
else if(zipf==1.0)
multiplier = log(n) + 0.577 ;
else if(zipf==2.0)
multiplier = (PI*PI)/6.0;
else if(zipf==3.0)
multiplier = 1.202;
else if(zipf==4.0)
multiplier = (PI*PI*PI*PI)/90.0;
else
{
/* compute multiplier (must be within given bounds) */
multiplier = 0;
for(i=1;i<=n;i++)
{
term = 1.0 / pow((double)i, zipf);
multiplier += term;
/* if later terms add very little we can stop */
if(term < EPSILON)
break;
}
}
return multiplier;
}
/******************************************************************%
RANDOM: Yields a random number from a Zipfian distribution with
a specified skewVal. Skew is an integer in the range 0..3
*******************************************************************/
/*
*
*/
long
SkewInt(long nLow, long nHigh, long nStream, double skewVal, long n)
/*
*
*/
{
double zipf;
double dRange;
long nTemp;
double multiplier;
double Czn;
long numDistinctValuesGenerated;
/* check for validity of skewVal */
if(skewVal < 0 || skewVal > 5)
zipf = 0; /* assume uniform */
else if(skewVal==5.0)
{
/* special case */
/* check if any values have been generated for this column */
if(NumDistinctValuesGenerated[nStream]==0)
{
/* pick a skew value to be used for this column*/
zipf = (int) UnifInt(0, 4, 0);
ColumnSkewValue[nStream] = zipf;
}
else
{
/* column skew value has been initialized before */
zipf = ColumnSkewValue[nStream];
}
}
else
{
/* skewVal is between 0 and 4 */
zipf = skewVal;
}
/* If no values have been generated for this stream as yet, get multiplier */
if(NumDistinctValuesGenerated[nStream]==0)
{
Multiplier[nStream] = GetMultiplier(n, zipf);
}
multiplier = Multiplier[nStream];
/*
* Check how many copies of the current value
* have already been generated for this stream.
* If we have generated enough, proceed to
* next value, and decide how many copies of
* this value should be generated.
*/
if(CurrentValueCounter[nStream] == CurrentValueTarget[nStream])
{
/* proceed to next value */
if (nStream < 0 || nStream > MAX_STREAM)
nStream = 0;
if (nLow == nHigh)
nTemp = nLow;
else
{
if (nLow > nHigh)
{
nTemp = nLow;
nLow = nHigh;
nHigh = nTemp;
}
dRange = (double) (nHigh - nLow + 1);
Seed[nStream] = NextRand(Seed[nStream]);
nTemp = (long) (((double) Seed[nStream] / dM) * (dRange));
nTemp += nLow;
CurrentValue[nStream] = nTemp;
}
}
else
{
/* return another copy of current value */
nTemp = CurrentValue[nStream];
CurrentValueCounter[nStream]++;
return nTemp;
}
/*
* check how many distinct values for this column
* have already been generated.
*/
numDistinctValuesGenerated = NumDistinctValuesGenerated[nStream] + 1;
if(n<1)
n = (nHigh - nLow + 1);
/*
* zipf is guaranteed to be in the range 0..4
* pick the multiplier
if(zipf == 0)
multiplier = n;
else if(zipf==1)
multiplier = log(n) + 0.577 ;
else if(zipf==2)
multiplier = (PI*PI)/6.0;
else if(zipf==3)
multiplier = 1.202;
else if(zipf==4)
multiplier = (PI*PI*PI*PI)/90.0;
*/
Czn = n/multiplier;
CurrentValueTarget[nStream]=
(long) (Czn/pow((double)numDistinctValuesGenerated, zipf));
/* ensure that there is at least one value*/
CurrentValueTarget[nStream] = (CurrentValueTarget[nStream] < 1) ? 1: CurrentValueTarget[nStream];
CurrentValueCounter[nStream] = 1;
NumDistinctValuesGenerated[nStream]++;
return nTemp;
}
/******************************************************************%
Returns the number of copies of the next value
in the given distribution. Skew is an integer in the range 0..4
*******************************************************************/
/*
*
*/
long
SkewIntCount(long nStream, double skewVal, long n)
/*
*
*/
{
double zipf;
double multiplier;
double Czn;
long numDistinctValuesGenerated;
long numCopies;
/* check for validity of skewVal */
if(skewVal < 0 || skewVal > 4)
zipf = 0; /* assume uniform */
else if(skewVal==5.0)
{
/* column skew value has been initialized before */
zipf = ColumnSkewValue[nStream];
}
else
{
/* skewVal is between 0 and 4 */
zipf = skewVal;
}
/*
* check how many distinct values for this column
* have already been generated.
*/
numDistinctValuesGenerated = NumDistinctValuesGenerated[nStream] + 1;
multiplier = Multiplier[nStream];
/*
* zipf is guaranteed to be in the range 0..4
* pick the multiplier
if(zipf == 0)
multiplier = n;
else if(zipf==1)
multiplier = log(n) + 0.577 ;
else if(zipf==2)
multiplier = (PI*PI)/6.0;
else if(zipf==3)
multiplier = 1.202;
else if(zipf==4)
multiplier = (PI*PI*PI*PI)/90.0;
*/
Czn = n/multiplier;
numCopies=
(long) (Czn/pow((double)numDistinctValuesGenerated, zipf));
/* ensure that there is at least one value*/
CurrentValueTarget[nStream] = (CurrentValueTarget[nStream] < 1) ? 1: CurrentValueTarget[nStream];
NumDistinctValuesGenerated[nStream]++;
return numCopies;
}