28extern double dlaran_(integer *seed);
29extern double dlaran_oag(integer *seed,
long increment);
31#define MAX_RAND_INT (1.0 * RAND_MAX)
42 return ((
float)(1.0 * rand() / MAX_RAND_INT));
54 return (lo + ((hi - lo) * rand()) / MAX_RAND_INT);
62 srand((
int)time((time_t)0));
75void r_theta_rand(
double *r,
double *theta,
double r_min,
double r_max) {
76 double area, sqr_r_min;
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);
84static short inhibitPermute = 0;
95 return inhibitPermute;
96 inhibitPermute = state;
110 long offset = input0 % 1000;
114 unsigned long bitMask[32] = {
153 for (i = 0; i < 31; i++) {
154 newValue += (input & bitMask[i]) ? bitMask[(i + offset) % 31] : 0;
156 if (newValue == input0) {
159 for (i = 0; i < 31; i++) {
160 newValue += (input & bitMask[i]) ? bitMask[(i + offset) % 31] : 0;
176 static short initialized = 0;
177 static integer seed[4] = {0, 0, 0, 0};
179 if (!initialized || iseed < 0) {
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;
196 bomb(
"random_1 not properly initialized", NULL);
198 return dlaran_(seed);
208 static short initialized = 0;
209 static integer seed[4] = {0, 0, 0, 0};
211 if (!initialized || iseed < 0) {
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;
222 bomb(
"random_2 not properly initialized", NULL);
224 return dlaran_(seed);
234 static short initialized = 0;
235 static integer seed[4] = {0, 0, 0, 0};
237 if (!initialized || iseed < 0) {
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;
248 bomb(
"random_3 not properly initialized", NULL);
250 return dlaran_(seed);
260 static short initialized = 0;
261 static integer seed[4] = {0, 0, 0, 0};
263 if (!initialized || iseed < 0) {
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;
274 bomb(
"random_4 not properly initialized", NULL);
276 return dlaran_(seed);
286 static short initialized = 0;
287 static integer seed[4] = {0, 0, 0, 0};
289 if (!initialized || iseed < 0) {
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;
300 bomb(
"random_5 not properly initialized", NULL);
302 return dlaran_(seed);
312 static short initialized = 0;
313 static integer seed[4] = {0, 0, 0, 0};
315 if (!initialized || iseed < 0) {
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;
326 bomb(
"random_6 not properly initialized", NULL);
328 return dlaran_(seed);
341double gauss_rn(
long iseed,
double (*urandom)(
long iseed1)) {
342 static long valueSaved = 0;
343 static double savedValue;
344 double urn1, urn2, sine, cosine, factor;
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;
360 return factor * sine;
379 double mean,
double sigma,
380 double limit_in_sigmas,
382 double (*urandom)(
long iseed)) {
385 if (limit_in_sigmas <= 0)
386 return (mean + sigma *
gauss_rn(0, urandom));
388 limit = limit_in_sigmas;
391 }
while (FABS(value) > limit);
392 return (sigma * value + mean);
407 double u1, u2, z = 0;
414 for (i = j = 0; i < points; i++) {
415 u1 = 2 * (data[i] - 0.5);
420#if defined(vxWorks) || defined(__rtems__)
421 fprintf(stderr,
"erf function is not implemented on this architecture\n");
424 z =
zeroNewton(erf, u2, 0.5, 1e-6, 500, 1e-12);
427 if (limit <= 0 || data[j] < limit) {
441int randomizeOrderCmp(
const void *p1,
const void *p2) {
446 if ((diff = rh1->randomValue - rh2->randomValue) > 0)
465long randomizeOrder(
char *ptr,
long size,
long length,
long iseed,
double (*urandom)(
long iseed1)) {
472 if (!(rh = malloc(
sizeof(*rh) * length)))
478 for (i = 0; i < length; i++) {
479 if (!(rh[i].buffer = malloc(size)))
481 memcpy(rh[i].buffer, ptr + i * size, size);
482 rh[i].randomValue = (*urandom)(0);
484 qsort((
void *)rh, length,
sizeof(*rh), randomizeOrderCmp);
486 for (i = 0; i < length; i++) {
487 memcpy(ptr + i * size, rh[i].buffer, size);
504 static short initialized = 0;
505 static long seed[4] = {0, 0, 0, 0};
507 if (!initialized || iseed < 0) {
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;
517 fprintf(stderr,
"random_oag not properly initialized\n");
521 return dlaran_oag(seed, increment);
534double gauss_rn_oag(
long iseed,
long increment,
double (*urandom)(
long iseed1,
long increment)) {
535 double urn1, urn2, sine, factor;
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;
560 double mean,
double sigma,
561 double limit_in_sigmas,
564 double (*urandom)(
long iseed,
long increment)) {
568 if (limit_in_sigmas <= 0)
569 return (mean + sigma *
gauss_rn_oag(0, increment, urandom));
571 limit = limit_in_sigmas;
576 if (FABS(value) <= limit)
578 }
while ((FABS(value) > limit) || (i < increment));
580 return (sigma * value + mean);
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
double random_5(long iseed)
Similar to random_4(), provides another independent random sequence.
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 gauss_rn(long iseed, double(*urandom)(long iseed1))
Generate a Gaussian-distributed random number with mean 0 and sigma 1.
double random_6(long iseed)
Similar to random_5(), provides another independent random sequence.
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].
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.
float drand(long dummy)
Generate a uniform random float in [0,1].
long convertSequenceToGaussianDistribution(double *data, long points, double limit)
Convert a sequence of uniformly distributed [0,1] values into a Gaussian-distributed sequence.
short inhibitRandomSeedPermutation(short state)
Enable or disable permutation of seed bits for random number generators.
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 random_oag(long iseed, long increment)
Generate a uniform random double in [0,1] using a seed and increment, optimized for certain applicati...
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.
long randomizeOrder(char *ptr, long size, long length, long iseed, double(*urandom)(long iseed1))
Randomize the order of an array of elements.
double rdrand(double lo, double hi)
Generate a uniform random double in [lo, hi].
double random_2(long iseed)
Similar to random_1(), provides a separate random sequence with its own seed handling.
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.
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.