I am making a random number generator that will either make a 0 or a 1. To do this, I simply used the code:
random = rand()%2; // generate a 0 or a 1
How can I make this generate a 1 or 0 with a 50% probability?
Any hints? Thanks
I am making a random number generator that will either make a 0 or a 1. To do this, I simply used the code:
random = rand()%2; // generate a 0 or a 1
How can I make this generate a 1 or 0 with a 50% probability?
Any hints? Thanks
You just did. There is a 50% chance that the number generated will be even or odd.
It doesn't get much more balanced than that.Code:#include <stdio.h> #include <time.h> int main ( void ) { int x=0,y=0,z=0; double d=0.0; srand(time(0)); for( x = 0; x < 10000000; x++ ) if(rand()%2) y++; else z++; printf("y = %d, z = %d, y %% == %f, z %% = %f", y, z, (float)y/x, (float)z/x ); return 0; }
Quzah.
Last edited by quzah; 04-16-2002 at 06:52 PM.
Hope is the first step on the road to disappointment.