Using a for loop, write a C++ program that simulates the tossing of a coin 100 times and counts how many of the tosses were heads, how many tails.  Your program should generate a random number in the range of 1 through 2.  If the random number is 1, the program should display “heads”.  If the random number is 2, the program should display “tails”.  After all the coins have been tossed the program should print out the count of how many heads and tails were tossed.  Recall: found in cstdlib rand( );  srand( x );    rand()  is a psuedorandom number generator - will eventually start repeating itself.   rand() will generate a series of seemingly random numbers depending on the number it starts with.  If rand starts with the same number each time, it will generate the same sequence of numbers.    srand(x) is a function in which x is the seed or starting number for the random number generator.  If srand is not used, rand assumes a seed of 1.    srand allows you to start with different starting points, allowing the random number generator to seem more random.   srand must only be used once or the random number generator may not be random.   In order to assure a different sequence of random numbers we use seed srand with time(0) found in ctime.   #include #include srand(time(0));

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.5: A Closer Look: Loop Programming Techniques
Problem 13E
icon
Related questions
Question
100%


Using a for loop, write a C++ program that simulates the tossing of a coin 100 times and counts how many of the tosses were heads, how many tails.  Your program should generate a random number in the range of 1 through 2.  If the random number is 1, the program should display “heads”.  If the random number is 2, the program should display “tails”.  After all the coins have been tossed the program should print out the count of how many heads and tails were tossed. 
Recall:
found in cstdlib
rand( ); 
srand( x );
 
 rand()  is a psuedorandom number generator - will eventually start repeating itself.
 
rand() will generate a series of seemingly random numbers depending on the number it starts with.  If rand starts with the same number each time, it will generate the same sequence of numbers. 
 
srand(x) is a function in which x is the seed or starting number for the random number generator.  If srand is not used, rand assumes a seed of 1. 
 
srand allows you to start with different starting points, allowing the random number generator to seem more random.   srand must only be used once or the random number generator may not be random.
 
In order to assure a different sequence of random numbers we use seed srand with time(0) found in ctime.  

#include<cstdlib>
#include<ctime>
srand(time(0));

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning