Random Boolean Value
It is called bad luck. Try it again.
On theory, there's 50% chance you get 0
, and 50 - 1
. You may want to try with different modulo - for example 100, to check if this works. And I'm sure it does.
You have just ran this code a few times, not enough.
Other idea to test it:
srand(time(0));
for( int i = 0; i < 1000000; ++i )
{
assert( 0 == ( rand() % 2 ) );
}
I would like to add that when you use srand(time(0));
the "random number" will always be the same in the same second. When I tried to run your program 10000 times and group it by uniq I saw that the number would not change within a second.
for i in `seq 1 10000`; do ./a.out; done | uniq -c
693 0
3415 1
675 0
673 1
665 0
674 1
668 0
711 1
694 0
673 1
459 0
I know this is an older question but I believe this answers the question properly.
Don't re-seed the the generator every time you run that code.
By seeding it to the same value every time, you're just gonna get the same "random" number. Remember this is a Pseudo-Random number generator, so based on the seed value, a "random" number will be generated. So if you seed it with the same number every time you're just gonna get the same number every time.
The solution is to call srand(time(NULL)) only once in your program execution. Then, each call to rand() will give you a different number every time.