Mail Archives: djgpp/1998/03/16/07:31:51
DeHackEd wrote:
>
> rand() and random() take no arguements. They return numbers from 0 to
> RAND_MAX or INT_MAX (not sure which). If you want to limit what they
> return, use a mod (rand()%10)command. If you want either 0 or 1, use
> random()&0x400 for safety, as random generators are not as random as
> one would hope.
you don't necessarily have to worry about this if you are just writing a
game, but a more proper way of generating a bernoulli (success/fail,
coin toss) random variable with probability of success p would be to use
#include <stdlib.h>
int bernoulli(double p)
{
return rand() > p*RAND_MAX;
}
-- Sinan
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA
mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/
- Raw text -