random isn't really random?
hi,
Just a quick question. I wrote a short code for generating 0, 1 randomly in order to simulate a coin-tossing game.
Code:
//This program will simulate 100 times
//of coin-tossings. Then it will calculate
//the frequency how many times it's HEAD or TAIL
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int flip(void); //return 0 for T(tail=0) and H(head=1)
int main()
{
int headCount=0;
int tailCount=0;
for (int i=1; i<=100; i++) //simulate flipping 100 times
{
switch(flip())
{
case (1):
cout << " " << "H";
headCount++;
break;
case (0):
cout << " " << "T";
tailCount++;
break;
}
if (i%10==0)
cout << endl;
}
cout << "Head " << headCount << endl;
cout << "Tail " << tailCount << endl;
return 0;
}
//Function that does fliping th coin
int flip(void)
{
int outcome;
outcome=rand()%2;
return outcome;
}
I compiled and execute the program, it just gave out 1 result all the time!!!
I'm wondering probably the way I use rand() is not good enough?
thanks!