This is a coin toss program. Every time I run the program it will give either all heads or all tails. It's never a mixture of both. Is it only seeding the generator once?
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
//func prototype*******************************************************
int coinToss();
//*********************************************************************
int main()
{
int num,
headsTails;
cout << "How many times would you like to run the coin toss?" << endl;
cin >> num;
for (int count = 0; count < num; count++)
{
headsTails = coinToss();
if (headsTails == 1)
{
cout << "The computer picked Heads." << endl;
}
else
{
cout << "The computer picked Tails." << endl;
}
}
return 0;
}
//func header**********************************************************
int coinToss()
{
int num,
max = 2,
min = 1;
int range = max - min + 1;
srand(time(NULL));
num = rand() % range + min;
return num;
}
//*********************************************************************
You're seeding the random number generator with the same value each time, because the time doesn't change quickly enough, so you're generating the exact same "random" value every time.