Coin toss (does this do the trick?)

Sep 27, 2015 at 9:19pm
so...ive been trying to do the coin toss exercise and all the code that ive seen online so far looks way more "elaborated"...so im not really sure about what i came up with, i ran it a few times and looks fine...i want to know if this does the trick (as you may have noticed im not looking for elegance points right now)


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>


using namespace std;


int main()
{
srand(time(0));

if ((rand()%4) == 0 || (rand()%4) == 2)
{
cout << "tails";
}

else
{
cout << "heads";
}

std::cin.clear(); // reset any error flags
std::cin.ignore(32767, '\n'); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user
return 0;
}
Sep 27, 2015 at 10:07pm
Well, your coin might be slightly biased to one or other side (because of usin modulus operator and bad RNG), but it will do the trick.

Oh, and if you will run your program twice in quich succession (in the same second) you will get same result. Also with many implementations your first number might be predictable (heads on odd seconds, tails on even)
Topic archived. No new replies allowed.