21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In all cases, we start with a number with uniform distribution using the API from<br />

Recipe 11.12.<br />

Note that these functions use math operations defined in the standard math library.<br />

On many platforms, you will have to link against the appropriate library (usually by<br />

adding -lm to your link line).<br />

#include <br />

#define NVCONST 1.7155277699141<br />

double spc_rand_normalvariate(double mu, double sigma) {<br />

double myr1, myr2, t1, t2;<br />

do {<br />

myr1 = spc_rand_real( );<br />

myr2 = spc_rand_real( );<br />

t1 = NVCONST * (myr1 - 0.5) / myr2;<br />

t2 = t1 * t1 / 4.0;<br />

} while (t2 > -log(myr2));<br />

return mu + t1 * sigma;<br />

}<br />

double spc_rand_lognormalvariate(double mu, double sigma) {<br />

return exp(spc_rand_normalvariate(mu, sigma));<br />

}<br />

double spc_rand_paretovariate(double alpha) {<br />

return 1.0 / pow(spc_rand_real( ), 1.0 / alpha);<br />

}<br />

double spc_rand_weibullvariate(double alpha, double beta) {<br />

return alpha * pow(-log(spc_rand_real( )), 1.0 / beta);<br />

}<br />

double spc_rand_expovariate(double lambda) {<br />

double myr = spc_rand_real( );<br />

while (myr

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!