Question: C++ programming question - Having the same output for the program, can't figure out why Let's say I enter 8 for the program, I should

C++ programming question - Having the same output for the program, can't figure out why

Let's say I enter 8 for the program, I should be able to get different output since the number is randomly generate it.

************************************

#include

#include

#include

int coinToss()

{

srand(time(0));

int temp = 1 + rand() % 2; // generate a random number from 1 and 2

if(temp == 1)

std::cout << "head" << std::endl; // if random number is 1, print "head"

else if(temp == 2)

std::cout << "tail" << std::endl; // if random number is 2, print "tail"

return temp; // return the random number

}

int main()

{

int input, result;

std::cout << "How many time you want to toss the coin: ";

std::cin >> input;

for (int i = 0; i < input; i++)

{

result = coinToss();

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!