I was trying to create a program that simulates tossing a coin to a user specified number of times and then say how many times heads appeared. However when i run my program it only seems to be looping once.
//generates a random number 1-10
int rdom_gen()
{
srand((unsigned)time(0));
int rdom_int;
rdom_int = (rand()%10) + 1;
return rdom_int;
}
//Takes a random number 1-10 and returns a 1 if less or equal 5
int onetesthead()
{
srand((unsigned)time(0));
int testing = rdom_gen();
if (testing <= 5 )
return 1;
else
return 0;
}
//This is supposed to be the loop that simmulates cointosses
int coinflip(int numtimes)
{
for (int i = 0; i < numtimes; i ++ )
{
int numofheads ;
numofheads = numofheads + onetesthead();
cout << "test loop";
cout << endl;
return numofheads;
}
}
int main()
{
int index;
cin >> index;
int numofheads = coinflip(index);
cout << numofheads;
cout << endl;
}