Random Boolean Value

Rich Byden picture Rich Byden · Dec 7, 2011 · Viewed 43.4k times · Source

I'm trying to generate a random int that is either 0 or 1 in C++. Right now, I receive a 0 every time I run this code, and I'm not sure why. What's the problem here?

#include <ctime>
#include <cstdlib>

srand(time(0));
int randomval = rand() % 2;
cout << randomval << endl;

Answer

Michael Krelin - hacker picture Michael Krelin - hacker · Dec 7, 2011

It is called bad luck. Try it again.