36 #include <sys/types.h>
50 #include <gsl/gsl_rng.h>
51 #include <gsl/gsl_randist.h>
52 #include <gsl/gsl_cdf.h>
53 #include <gsl/gsl_sf_gamma.h>
54 #include <gsl/gsl_math.h>
55 #include <gsl/gsl_errno.h>
65 fesetround(FE_TONEAREST);
69 const gsl_rng_type * T;
72 flow->
r = gsl_rng_alloc (T);
77 DEBUG_MSG(LOG_WARNING,
"client did not supply random seed value");
78 int data = open(
"/dev/urandom", O_RDONLY);
79 rc = read(
data, &seed,
sizeof (
long) );
82 crit(
"read /dev/urandom failed");
86 gsl_rng_set (
flow->
r, seed);
87 DEBUG_MSG(LOG_WARNING,
"initalized local libgsl random functions for "
88 "flow %d with seed %lu, gsl generator is: %s",
91 srand((
unsigned)seed);
92 DEBUG_MSG(LOG_WARNING,
"initalized posix random functions with seed "
93 "%u", (
unsigned)seed);
100 gsl_rng_free(
flow->
r);
107 static inline double rn_uniform(
void)
109 return (
double)rand();
112 static inline double rn_uniform_zero_to_one(
void)
114 return (rn_uniform()/(RAND_MAX + 1.0));
117 static inline double rn_uniform_minusone_to_one(
void)
119 return (rn_uniform()/(RAND_MAX/2.0) - 1.0);
126 gsl_rng * r =
flow->
r;
127 return gsl_ran_exponential(r, mu);
130 return -log(rn_uniform())+mu;
140 gsl_rng * r =
flow->
r;
141 return gsl_ran_flat(r, minval, maxval);
144 const double x = rn_uniform_zero_to_one();
145 return ((maxval-minval) * x) + minval;
150 const double sigma_square)
153 const gsl_rng * r =
flow->
r;
154 return gsl_ran_gaussian (r, sigma_square) + mu;
157 const double x = rn_uniform_minusone_to_one();
158 return (1.0 / sqrt(2.0*M_PI*sigma_square)) *
159 exp((-pow ((x-mu),2)) / (2 * sigma_square));
167 gsl_rng * r =
flow->
r;
168 return gsl_ran_lognormal (r, zeta, sigma);
182 gsl_rng * r =
flow->
r;
183 return gsl_ran_bernoulli (r, p);
186 return rn_uniform_zero_to_one() <= p;
194 gsl_rng * r =
flow->
r;
195 return gsl_ran_pareto (r, k, x_min);
198 const double x = rn_uniform();
202 return (k/x_min) * pow (x_min/x,k+1);
210 gsl_rng * r =
flow->
r;
211 return gsl_ran_weibull (r, alpha, beta);
214 const double x = rn_uniform_zero_to_one();
215 return alpha * beta * pow (x,beta-1.0) * exp(-alpha * pow(x,beta));
222 gsl_rng * r =
flow->
r;
223 return gsl_ran_chisq(r, nu);