2015-03-23 21:03:14 +00:00
|
|
|
/*
|
2015-02-26 15:02:31 +00:00
|
|
|
PURPOSE: (Generate a random number consistent with a Gaussian distribution.)
|
|
|
|
|
|
|
|
ASSUMPTIONS AND LIMITATIONS: ((State spaces for independent processes requiring random numbers are not tracked.)
|
|
|
|
(Range is specified in standard deviations; i.e. if a range of 3 sigma is specified, then NO numbers greater than
|
2015-03-23 21:03:14 +00:00
|
|
|
the plus/minus 3 sigma value will be returned.) (Automatic Seed))
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2015-03-23 21:03:14 +00:00
|
|
|
PROGRAMMERS: (((Robert W. Bailey) (LinCom Corp) (4/15/91) (Trick-CR-00009) (SAFR Model))) */
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-06-01 15:28:29 +00:00
|
|
|
#include "trick/trick_math.h"
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define MAX_VALUE 32768.0 /* 32768 = 2**15 */
|
|
|
|
|
|
|
|
double drandom_gaussian( /* Return: Random number consistent with Gaussian distribution */
|
|
|
|
double std_deviation, /* In: One standard deviation */
|
|
|
|
int range)
|
|
|
|
{ /* In: Plus/Minus range for Gaussian output in standard deviations */
|
|
|
|
|
|
|
|
int i;
|
|
|
|
double out;
|
|
|
|
double number = 0.0;
|
|
|
|
|
|
|
|
for (i = 0; i < 2 * range; i++) {
|
|
|
|
/* Call rand() 2*range times to get plus/minus range sigma output */
|
|
|
|
|
|
|
|
/* Normalize output of rand to the range of [-0.5,0.5] */
|
|
|
|
#ifdef __linux
|
|
|
|
out = (((double) rand()) / RAND_MAX) - 0.5;
|
|
|
|
#elif __APPLE__
|
|
|
|
out = (((double) rand()) / LONG_MAX) - 0.5;
|
|
|
|
#else
|
|
|
|
out = (((double) rand()) / MAX_VALUE) - 0.5;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Scale normalized output by one sigma value and sum to previous normalized values */
|
|
|
|
number += (std_deviation * out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resulting summed value is a gaussian random number */
|
|
|
|
return (number);
|
|
|
|
}
|