C: Random Number Generation - What (If Anything) Is Wrong With This

Posted by raoulcousins on Stack Overflow See other posts from Stack Overflow or by raoulcousins
Published on 2010-04-06T16:07:26Z Indexed on 2010/04/06 16:13 UTC
Read the original article Hit count: 261

For a simple simulation in C, I need to generate exponential random variables. I remember reading somewhere (but I can't find it now, and I don't remember why) that using the rand() function to generate random integers in a fixed range would generate non-uniformly distributed integers. Because of this, I'm wondering if this code might have a similar problem:

//generate u ~ U[0,1]
u = (   (double)rand() / ((double)(RAND_MAX));
//inverse of exponential CDF to get exponential random variable
expon = -log(1-u) * mean;

Thank you!

© Stack Overflow or respective owner

Related posts about c

    Related posts about random-number-generator