SDDSlib
Loading...
Searching...
No Matches
drand.c File Reference

Random number generation functions providing various distributions (uniform, Gaussian) and related utilities (seeding, ordering, etc.). More...

#include "mdb.h"
#include <time.h>
#include <stdlib.h>
#include "f2c.h"

Go to the source code of this file.

Classes

struct  RANDOMIZATION_HOLDER
 

Macros

#define MAX_RAND_INT   (1.0 * RAND_MAX)
 

Functions

double dlaran_ (integer *seed)
 
double dlaran_oag (integer *seed, long increment)
 
float drand (long dummy)
 Generate a uniform random float in [0,1].
 
double rdrand (double lo, double hi)
 Generate a uniform random double in [lo, hi].
 
void tseed ()
 
void r_theta_rand (double *r, double *theta, double r_min, double r_max)
 Generate a random point (r, θ) within an annulus defined by [r_min, r_max].
 
short inhibitRandomSeedPermutation (short state)
 Enable or disable permutation of seed bits for random number generators.
 
long permuteSeedBitOrder (long input0)
 Permute the bit order of a seed value to improve randomness.
 
double random_1 (long iseed)
 Generate a uniform random double in [0,1] using a custom seed initialization.
 
double random_2 (long iseed)
 Similar to random_1(), provides a separate random sequence with its own seed handling.
 
double random_3 (long iseed)
 Similar to random_2(), provides another independent random sequence.
 
double random_4 (long iseed)
 Similar to random_3(), provides another independent random sequence.
 
double random_5 (long iseed)
 Similar to random_4(), provides another independent random sequence.
 
double random_6 (long iseed)
 Similar to random_5(), provides another independent random sequence.
 
double gauss_rn (long iseed, double(*urandom)(long iseed1))
 Generate a Gaussian-distributed random number with mean 0 and sigma 1.
 
double gauss_rn_lim (double mean, double sigma, double limit_in_sigmas, double(*urandom)(long iseed))
 Generate a Gaussian-distributed random number with specified mean, sigma, and optional cutoff.
 
long convertSequenceToGaussianDistribution (double *data, long points, double limit)
 Convert a sequence of uniformly distributed [0,1] values into a Gaussian-distributed sequence.
 
int randomizeOrderCmp (const void *p1, const void *p2)
 
long randomizeOrder (char *ptr, long size, long length, long iseed, double(*urandom)(long iseed1))
 Randomize the order of an array of elements.
 
double random_oag (long iseed, long increment)
 Generate a uniform random double in [0,1] using a seed and increment, optimized for certain applications.
 
double gauss_rn_oag (long iseed, long increment, double(*urandom)(long iseed1, long increment))
 Generate a Gaussian-distributed random number using the random_oag approach.
 
double gauss_rn_lim_oag (double mean, double sigma, double limit_in_sigmas, long increment, double(*urandom)(long iseed, long increment))
 Generate a Gaussian-distributed random number with mean, sigma, and optional cutoff using oag RNG.
 

Variables

static short inhibitPermute = 0
 

Detailed Description

Random number generation functions providing various distributions (uniform, Gaussian) and related utilities (seeding, ordering, etc.).

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file drand.c.

Macro Definition Documentation

◆ MAX_RAND_INT

#define MAX_RAND_INT   (1.0 * RAND_MAX)

Definition at line 31 of file drand.c.

Function Documentation

◆ convertSequenceToGaussianDistribution()

long convertSequenceToGaussianDistribution ( double * data,
long points,
double limit )

Convert a sequence of uniformly distributed [0,1] values into a Gaussian-distributed sequence.

Uses the inverse error function (erf) to transform uniform data into Gaussian distributed data. Values that exceed the given limit are discarded.

Parameters
[in,out]dataArray of input values in [0,1] to be converted.
[in]pointsNumber of values in the array.
[in]limitUpper cutoff in standard deviations (if <= 0, no cutoff).
Returns
The number of successfully converted data points.

Definition at line 406 of file drand.c.

406 {
407 double u1, u2, z = 0;
408 long i, j;
409
410 if (!points)
411 return 0;
412 if (!data)
413 return 0;
414 for (i = j = 0; i < points; i++) {
415 u1 = 2 * (data[i] - 0.5);
416 if (u1 < 0)
417 u2 = -u1;
418 else
419 u2 = u1;
420#if defined(vxWorks) || defined(__rtems__)
421 fprintf(stderr, "erf function is not implemented on this architecture\n");
422 exit(1);
423#else
424 z = zeroNewton(erf, u2, 0.5, 1e-6, 500, 1e-12);
425#endif
426 data[j] = z * SQRT2;
427 if (limit <= 0 || data[j] < limit) {
428 if (u1 < 0)
429 data[j] = -data[j];
430 j++;
431 }
432 }
433 return j;
434}
double zeroNewton(double(*fn)(), double value, double x_i, double dx, long n_passes, double _zero)
Finds the zero of a function using Newton's method with numerical derivative computation.
Definition zeroNewton.c:33

◆ dlaran_()

double dlaran_ ( integer * seed)
extern

Definition at line 33 of file dlaran.c.

33 {
34 /* System generated locals */
35 doublereal ret_val;
36
37 /* Local variables */
38 integer it1, it2, it3, it4;
39
40 /* Parameter adjustments */
41 /*--iseed;*/
42
43 /* Function Body */
44
45 /* -- LAPACK auxiliary routine (version 2.0) -- */
46 /* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd., */
47 /* Courant Institute, Argonne National Lab, and Rice University */
48 /* February 29, 1992 */
49
50 /* .. Array Arguments .. */
51 /* .. */
52
53 /* Purpose */
54 /* ======= */
55
56 /* DLARAN returns a random real number from a uniform (0,1) */
57 /* distribution. */
58
59 /* Arguments */
60 /* ========= */
61
62 /* ISEED (input/output) INTEGER array, dimension (4) */
63 /* On entry, the seed of the random number generator; the array
64*/
65 /* elements must be between 0 and 4095, and ISEED(4) must be */
66 /* odd. */
67 /* On exit, the seed is updated. */
68
69 /* Further Details */
70 /* =============== */
71
72 /* This routine uses a multiplicative congruential method with modulus */
73
74 /* 2**48 and multiplier 33952834046453 (see G.S.Fishman, */
75 /* 'Multiplicative congruential random number generators with modulus */
76 /* 2**b: an exhaustive analysis for b = 32 and a partial analysis for */
77 /* b = 48', Math. Comp. 189, pp 331-344, 1990). */
78
79 /* 48-bit integers are stored in 4 integer array elements with 12 bits */
80
81 /* per element. Hence the routine is portable across machines with */
82 /* integers of 32 bits or more. */
83
84 /* =====================================================================
85*/
86
87 /* .. Parameters .. */
88 /* .. */
89 /* .. Local Scalars .. */
90 /* .. */
91 /* .. Intrinsic Functions .. */
92 /* .. */
93 /* .. Executable Statements .. */
94
95 /* multiply the seed by the multiplier modulo 2**48 */
96
97 it4 = iseed[3] * 2549;
98 it3 = it4 / 4096;
99 it4 -= it3 << 12;
100 it3 = it3 + iseed[2] * 2549 + iseed[3] * 2508;
101 it2 = it3 / 4096;
102 it3 -= it2 << 12;
103 it2 = it2 + iseed[1] * 2549 + iseed[2] * 2508 + iseed[3] * 322;
104 it1 = it2 / 4096;
105 it2 -= it1 << 12;
106 it1 = it1 + iseed[0] * 2549 + iseed[1] * 2508 + iseed[2] * 322 + iseed[3] * 494;
107 it1 %= 4096;
108
109 /* return updated seed */
110
111 iseed[0] = it1;
112 iseed[1] = it2;
113 iseed[2] = it3;
114 iseed[3] = it4;
115
116 /* convert 48-bit integer to a real number in the interval (0,1) */
117
118 ret_val = ((doublereal)it1 + ((doublereal)it2 + ((doublereal)it3 + (doublereal)it4 * 2.44140625e-4) * 2.44140625e-4) * 2.44140625e-4) * 2.44140625e-4;
119 return ret_val;
120
121 /* End of DLARAN */
122
123} /* dlaran_ */

◆ dlaran_oag()

double dlaran_oag ( integer * seed,
long increment )
extern

Definition at line 125 of file dlaran.c.

125 {
126 doublereal ret_val;
127
128 integer it1, it2, it3, it4, i;
129
130 if (increment < 1)
131 increment = 1;
132 for (i = 0; i < increment; i++) {
133 it4 = iseed[3] * 2549;
134 it3 = it4 / 4096;
135 it4 -= it3 << 12;
136 it3 = it3 + iseed[2] * 2549 + iseed[3] * 2508;
137 it2 = it3 / 4096;
138 it3 -= it2 << 12;
139 it2 = it2 + iseed[1] * 2549 + iseed[2] * 2508 + iseed[3] * 322;
140 it1 = it2 / 4096;
141 it2 -= it1 << 12;
142 it1 = it1 + iseed[0] * 2549 + iseed[1] * 2508 + iseed[2] * 322 + iseed[3] * 494;
143 it1 %= 4096;
144
145 iseed[0] = it1;
146 iseed[1] = it2;
147 iseed[2] = it3;
148 iseed[3] = it4;
149 }
150
151 ret_val = ((doublereal)it1 + ((doublereal)it2 + ((doublereal)it3 + (doublereal)it4 * 2.44140625e-4) * 2.44140625e-4) * 2.44140625e-4) * 2.44140625e-4;
152 return ret_val;
153}

◆ drand()

float drand ( long dummy)

Generate a uniform random float in [0,1].

Uses the standard C rand() function. The parameter dummy is unused.

Parameters
[in]dummyUnused parameter.
Returns
A random float in [0,1].

Definition at line 41 of file drand.c.

41 {
42 return ((float)(1.0 * rand() / MAX_RAND_INT));
43}

◆ gauss_rn()

double gauss_rn ( long iseed,
double(* urandom )(long iseed1) )

Generate a Gaussian-distributed random number with mean 0 and sigma 1.

Uses the given uniform random generator urandom to produce Gaussian deviates via the Box–Muller transform.

Parameters
[in]iseedIf negative, re-initializes the uniform RNG.
[in]urandomPointer to a uniform random number generator function.
Returns
A Gaussian random deviate with mean 0 and sigma 1.

Definition at line 341 of file drand.c.

341 {
342 static long valueSaved = 0;
343 static double savedValue;
344 double urn1, urn2, sine, cosine, factor;
345
346 if (iseed < 0)
347 (*urandom)(iseed);
348 if (!valueSaved) {
349 urn1 = (*urandom)(0);
350 urn2 = (*urandom)(0);
351 factor = sqrt(-2 * log(urn1));
352 cosine = cos(PIx2 * urn2);
353 sine = sin(PIx2 * urn2);
354 savedValue = factor * cosine;
355 /* to use saved values, set this to 1 instead
356 * I've disabled this feature as it doesn't work properly with multiple
357 * urandom's.
358 */
359 valueSaved = 0;
360 return factor * sine;
361 } else {
362 valueSaved = 0;
363 return savedValue;
364 }
365}

◆ gauss_rn_lim()

double gauss_rn_lim ( double mean,
double sigma,
double limit_in_sigmas,
double(* urandom )(long iseed) )

Generate a Gaussian-distributed random number with specified mean, sigma, and optional cutoff.

If limit_in_sigmas > 0, values are regenerated until the deviate falls within ±limit_in_sigmas*sigma.

Parameters
[in]meanMean of the Gaussian distribution.
[in]sigmaStandard deviation of the Gaussian distribution.
[in]limit_in_sigmasCutoff in multiples of sigma (if <= 0, no cutoff).
[in]urandomPointer to a uniform random number generator function.
Returns
A Gaussian random deviate meeting the specified conditions.

Definition at line 378 of file drand.c.

382 {
383 double limit, value;
384
385 if (limit_in_sigmas <= 0)
386 return (mean + sigma * gauss_rn(0, urandom));
387
388 limit = limit_in_sigmas;
389 do {
390 value = gauss_rn(0, urandom);
391 } while (FABS(value) > limit);
392 return (sigma * value + mean);
393}
double gauss_rn(long iseed, double(*urandom)(long iseed1))
Generate a Gaussian-distributed random number with mean 0 and sigma 1.
Definition drand.c:341

◆ gauss_rn_lim_oag()

double gauss_rn_lim_oag ( double mean,
double sigma,
double limit_in_sigmas,
long increment,
double(* urandom )(long iseed, long increment) )

Generate a Gaussian-distributed random number with mean, sigma, and optional cutoff using oag RNG.

If limit_in_sigmas > 0, values are regenerated until they fall within the cutoff range.

Parameters
[in]meanMean of the Gaussian distribution.
[in]sigmaStandard deviation of the Gaussian distribution.
[in]limit_in_sigmasCutoff in multiples of sigma (if <= 0, no cutoff).
[in]incrementIncrement step for random number generation.
[in]urandomPointer to an oag-style uniform random number generator function.
Returns
A Gaussian random deviate meeting the specified conditions.

Definition at line 559 of file drand.c.

564 {
565 double limit, value;
566 long i;
567
568 if (limit_in_sigmas <= 0)
569 return (mean + sigma * gauss_rn_oag(0, increment, urandom));
570
571 limit = limit_in_sigmas;
572 i = 0;
573
574 do {
575 value = gauss_rn_oag(0, 1, urandom);
576 if (FABS(value) <= limit)
577 i++;
578 } while ((FABS(value) > limit) || (i < increment));
579
580 return (sigma * value + mean);
581}
double gauss_rn_oag(long iseed, long increment, double(*urandom)(long iseed1, long increment))
Generate a Gaussian-distributed random number using the random_oag approach.
Definition drand.c:534

◆ gauss_rn_oag()

double gauss_rn_oag ( long iseed,
long increment,
double(* urandom )(long iseed1, long increment) )

Generate a Gaussian-distributed random number using the random_oag approach.

Uses a modified Box–Muller method to generate Gaussian deviates from the urandom function provided.

Parameters
[in]iseedSeed for initialization if negative.
[in]incrementIncrement step for random number generation.
[in]urandomPointer to an oag-style uniform random number generator function.
Returns
A Gaussian random deviate with mean 0 and sigma 1.

Definition at line 534 of file drand.c.

534 {
535 double urn1, urn2, sine, factor;
536
537 if (increment < 1)
538 increment = 1;
539 increment = ((increment - 1) * 2) + 1;
540 urn1 = (*urandom)(iseed, increment);
541 urn2 = (*urandom)(0, 1);
542 factor = sqrt(-2 * log(urn1));
543 sine = sin(PIx2 * urn2);
544 return factor * sine;
545}

◆ inhibitRandomSeedPermutation()

short inhibitRandomSeedPermutation ( short state)

Enable or disable permutation of seed bits for random number generators.

If state >= 0, sets the inhibitPermute flag. Otherwise, returns the current state without changing it.

Parameters
[in]stateNew state for inhibition (0 = no inhibition, 1 = inhibited).
Returns
The current state of the inhibitPermute flag.

Definition at line 93 of file drand.c.

93 {
94 if (state < 0)
95 return inhibitPermute;
96 inhibitPermute = state;
97 return state;
98}

◆ permuteSeedBitOrder()

long permuteSeedBitOrder ( long input0)

Permute the bit order of a seed value to improve randomness.

Applies a permutation of the seed bits to avoid predictable patterns. If inhibition is enabled, returns the original input.

Parameters
[in]input0The seed value to permute.
Returns
The permuted seed value.

Definition at line 109 of file drand.c.

109 {
110 long offset = input0 % 1000;
111 long newValue;
112 long i;
113 unsigned long input;
114 unsigned long bitMask[32] = {
115 0x00000001UL,
116 0x00000002UL,
117 0x00000004UL,
118 0x00000008UL,
119 0x00000010UL,
120 0x00000020UL,
121 0x00000040UL,
122 0x00000080UL,
123 0x00000100UL,
124 0x00000200UL,
125 0x00000400UL,
126 0x00000800UL,
127 0x00001000UL,
128 0x00002000UL,
129 0x00004000UL,
130 0x00008000UL,
131 0x00010000UL,
132 0x00020000UL,
133 0x00040000UL,
134 0x00080000UL,
135 0x00100000UL,
136 0x00200000UL,
137 0x00400000UL,
138 0x00800000UL,
139 0x01000000UL,
140 0x02000000UL,
141 0x04000000UL,
142 0x08000000UL,
143 0x10000000UL,
144 0x20000000UL,
145 0x40000000UL,
146 0x08000000UL,
147 };
148 if (inhibitPermute)
149 return input0;
150
151 input = input0;
152 newValue = 0;
153 for (i = 0; i < 31; i++) {
154 newValue += (input & bitMask[i]) ? bitMask[(i + offset) % 31] : 0;
155 }
156 if (newValue == input0) {
157 offset += 1;
158 newValue = 0;
159 for (i = 0; i < 31; i++) {
160 newValue += (input & bitMask[i]) ? bitMask[(i + offset) % 31] : 0;
161 }
162 }
163 return newValue;
164}

◆ r_theta_rand()

void r_theta_rand ( double * r,
double * theta,
double r_min,
double r_max )

Generate a random point (r, θ) within an annulus defined by [r_min, r_max].

The angle θ is chosen uniformly in [0, 2π), and r is chosen so that the area distribution is uniform.

Parameters
[out]rPointer to store the generated radius.
[out]thetaPointer to store the generated angle in radians.
[in]r_minThe inner radius of the annulus.
[in]r_maxThe outer radius of the annulus.

Definition at line 75 of file drand.c.

75 {
76 double area, sqr_r_min;
77
78 *theta = rdrand(0.0, PIx2);
79 sqr_r_min = sqr(r_min);
80 area = rdrand(0.0, sqr(r_max) - sqr_r_min);
81 *r = sqrt(area + sqr_r_min);
82}
double rdrand(double lo, double hi)
Generate a uniform random double in [lo, hi].
Definition drand.c:52

◆ random_1()

double random_1 ( long iseed)

Generate a uniform random double in [0,1] using a custom seed initialization.

Initializes the random number generator if needed, and then produces a double in [0,1]. Negative iseed values are used to re-initialize the sequence.

Parameters
[in]iseedSeed for initialization if negative, otherwise ignored after first call.
Returns
A random double in [0,1].

Definition at line 175 of file drand.c.

175 {
176 static short initialized = 0;
177 static integer seed[4] = {0, 0, 0, 0};
178
179 if (!initialized || iseed < 0) {
180 if (iseed < 0)
181 iseed = -iseed;
182 iseed = permuteSeedBitOrder(iseed);
183 random_2(-(iseed + 2));
184 random_3(-(iseed + 4));
185 random_4(-(iseed + 6));
186 random_5(-(iseed + 8));
187 random_6(-(iseed + 10));
188 iseed = (iseed / 2) * 2 + 1;
189 seed[3] = (iseed & 4095);
190 seed[2] = (iseed >>= 12) & 4095;
191 seed[1] = (iseed >>= 12) & 4095;
192 seed[0] = (iseed >>= 12) & 4095;
193 initialized = 1;
194 }
195 if (!initialized)
196 bomb("random_1 not properly initialized", NULL);
197
198 return dlaran_(seed);
199}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
double random_5(long iseed)
Similar to random_4(), provides another independent random sequence.
Definition drand.c:285
long permuteSeedBitOrder(long input0)
Permute the bit order of a seed value to improve randomness.
Definition drand.c:109
double random_6(long iseed)
Similar to random_5(), provides another independent random sequence.
Definition drand.c:311
double random_3(long iseed)
Similar to random_2(), provides another independent random sequence.
Definition drand.c:233
double random_4(long iseed)
Similar to random_3(), provides another independent random sequence.
Definition drand.c:259
double random_2(long iseed)
Similar to random_1(), provides a separate random sequence with its own seed handling.
Definition drand.c:207

◆ random_2()

double random_2 ( long iseed)

Similar to random_1(), provides a separate random sequence with its own seed handling.

Parameters
[in]iseedSeed for initialization if negative.
Returns
A random double in [0,1].

Definition at line 207 of file drand.c.

207 {
208 static short initialized = 0;
209 static integer seed[4] = {0, 0, 0, 0};
210
211 if (!initialized || iseed < 0) {
212 if (iseed < 0)
213 iseed = -iseed;
214 iseed = permuteSeedBitOrder(iseed);
215 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
216 seed[2] = (iseed >>= 12) & 4095;
217 seed[1] = (iseed >>= 12) & 4095;
218 seed[0] = (iseed >>= 12) & 4095;
219 initialized = 1;
220 }
221 if (!initialized)
222 bomb("random_2 not properly initialized", NULL);
223
224 return dlaran_(seed);
225}

◆ random_3()

double random_3 ( long iseed)

Similar to random_2(), provides another independent random sequence.

Parameters
[in]iseedSeed for initialization if negative.
Returns
A random double in [0,1].

Definition at line 233 of file drand.c.

233 {
234 static short initialized = 0;
235 static integer seed[4] = {0, 0, 0, 0};
236
237 if (!initialized || iseed < 0) {
238 if (iseed < 0)
239 iseed = -iseed;
240 iseed = permuteSeedBitOrder(iseed);
241 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
242 seed[2] = (iseed >>= 12) & 4095;
243 seed[1] = (iseed >>= 12) & 4095;
244 seed[0] = (iseed >>= 12) & 4095;
245 initialized = 1;
246 }
247 if (!initialized)
248 bomb("random_3 not properly initialized", NULL);
249
250 return dlaran_(seed);
251}

◆ random_4()

double random_4 ( long iseed)

Similar to random_3(), provides another independent random sequence.

Parameters
[in]iseedSeed for initialization if negative.
Returns
A random double in [0,1].

Definition at line 259 of file drand.c.

259 {
260 static short initialized = 0;
261 static integer seed[4] = {0, 0, 0, 0};
262
263 if (!initialized || iseed < 0) {
264 if (iseed < 0)
265 iseed = -iseed;
266 iseed = permuteSeedBitOrder(iseed);
267 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
268 seed[2] = (iseed >>= 12) & 4095;
269 seed[1] = (iseed >>= 12) & 4095;
270 seed[0] = (iseed >>= 12) & 4095;
271 initialized = 1;
272 }
273 if (!initialized)
274 bomb("random_4 not properly initialized", NULL);
275
276 return dlaran_(seed);
277}

◆ random_5()

double random_5 ( long iseed)

Similar to random_4(), provides another independent random sequence.

Parameters
[in]iseedSeed for initialization if negative.
Returns
A random double in [0,1].

Definition at line 285 of file drand.c.

285 {
286 static short initialized = 0;
287 static integer seed[4] = {0, 0, 0, 0};
288
289 if (!initialized || iseed < 0) {
290 if (iseed < 0)
291 iseed = -iseed;
292 iseed = permuteSeedBitOrder(iseed);
293 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
294 seed[2] = (iseed >>= 12) & 4095;
295 seed[1] = (iseed >>= 12) & 4095;
296 seed[0] = (iseed >>= 12) & 4095;
297 initialized = 1;
298 }
299 if (!initialized)
300 bomb("random_5 not properly initialized", NULL);
301
302 return dlaran_(seed);
303}

◆ random_6()

double random_6 ( long iseed)

Similar to random_5(), provides another independent random sequence.

Parameters
[in]iseedSeed for initialization if negative.
Returns
A random double in [0,1].

Definition at line 311 of file drand.c.

311 {
312 static short initialized = 0;
313 static integer seed[4] = {0, 0, 0, 0};
314
315 if (!initialized || iseed < 0) {
316 if (iseed < 0)
317 iseed = -iseed;
318 iseed = permuteSeedBitOrder(iseed);
319 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
320 seed[2] = (iseed >>= 12) & 4095;
321 seed[1] = (iseed >>= 12) & 4095;
322 seed[0] = (iseed >>= 12) & 4095;
323 initialized = 1;
324 }
325 if (!initialized)
326 bomb("random_6 not properly initialized", NULL);
327
328 return dlaran_(seed);
329}

◆ random_oag()

double random_oag ( long iseed,
long increment )

Generate a uniform random double in [0,1] using a seed and increment, optimized for certain applications.

Uses a custom random number generator implemented in Fortran (dlaran_oag).

Parameters
[in]iseedSeed for initialization if negative.
[in]incrementIncrement to apply for each call.
Returns
A random double in [0,1].

Definition at line 503 of file drand.c.

503 {
504 static short initialized = 0;
505 static long seed[4] = {0, 0, 0, 0};
506
507 if (!initialized || iseed < 0) {
508 if (iseed < 0)
509 iseed = -iseed;
510 seed[3] = ((iseed & 4095) / 2) * 2 + 1;
511 seed[2] = (iseed >>= 12) & 4095;
512 seed[1] = (iseed >>= 12) & 4095;
513 seed[0] = (iseed >>= 12) & 4095;
514 initialized = 1;
515 }
516 if (!initialized) {
517 fprintf(stderr, "random_oag not properly initialized\n");
518 exit(1);
519 }
520
521 return dlaran_oag(seed, increment);
522}

◆ randomizeOrder()

long randomizeOrder ( char * ptr,
long size,
long length,
long iseed,
double(* urandom )(long iseed1) )

Randomize the order of an array of elements.

Shuffles the elements of an array using a provided uniform random generator.

Parameters
[in,out]ptrPointer to the array to randomize.
[in]sizeSize of each element in bytes.
[in]lengthNumber of elements in the array.
[in]iseedSeed for initialization if negative.
[in]urandomPointer to a uniform random number generator function.
Returns
Non-zero if successful, zero otherwise.

Definition at line 465 of file drand.c.

465 {
467 long i;
468 if (length < 2)
469 return 1;
470 if (!ptr)
471 return 0;
472 if (!(rh = malloc(sizeof(*rh) * length)))
473 return 0;
474 if (!urandom)
475 return 0;
476 if (iseed < 0)
477 (*urandom)(iseed);
478 for (i = 0; i < length; i++) {
479 if (!(rh[i].buffer = malloc(size)))
480 return 0;
481 memcpy(rh[i].buffer, ptr + i * size, size);
482 rh[i].randomValue = (*urandom)(0);
483 }
484 qsort((void *)rh, length, sizeof(*rh), randomizeOrderCmp);
485
486 for (i = 0; i < length; i++) {
487 memcpy(ptr + i * size, rh[i].buffer, size);
488 free(rh[i].buffer);
489 }
490 free(rh);
491 return 1;
492}

◆ randomizeOrderCmp()

int randomizeOrderCmp ( const void * p1,
const void * p2 )

Definition at line 441 of file drand.c.

441 {
442 RANDOMIZATION_HOLDER *rh1, *rh2;
443 double diff;
444 rh1 = (RANDOMIZATION_HOLDER *)p1;
445 rh2 = (RANDOMIZATION_HOLDER *)p2;
446 if ((diff = rh1->randomValue - rh2->randomValue) > 0)
447 return 1;
448 if (diff < 0)
449 return -1;
450 return 0;
451}

◆ rdrand()

double rdrand ( double lo,
double hi )

Generate a uniform random double in [lo, hi].

Parameters
[in]loThe lower bound of the range.
[in]hiThe upper bound of the range.
Returns
A random double in [lo, hi].

Definition at line 52 of file drand.c.

53{
54 return (lo + ((hi - lo) * rand()) / MAX_RAND_INT);
55}

◆ tseed()

void tseed ( )

Definition at line 61 of file drand.c.

61 {
62 srand((int)time((time_t)0));
63}

Variable Documentation

◆ inhibitPermute

short inhibitPermute = 0
static

Definition at line 84 of file drand.c.