Question
CSC100AA and CIS162AA Chapter 5 : Looping Ch 5 Program 4 Random Check RandomCheck.cpp (10 pts) In this program, we will be checking the randomness
CSC100AA and CIS162AA Chapter 5 : Looping Ch 5 Program 4 Random Check
RandomCheck.cpp (10 pts) In this program, we will be checking the randomness of the C++ random generator. Download the starting file RandomCheck.cpp. Compile and run it. Evaluate the code.
Add a for loop that will:
generate a number of 1's and 2's as requested by the user
add up the number 1's and 2's generated and store the values in variables numberOfOnes and numberOfTwos
Here is an example of output. The user requested 50 values.
Enter the number of values to be generated : 50
Ones generated : 26 Percent : 52.00%
Twos generated : 24 Percent: 48.00% Include output from this Test Data : 5, 500, 50000, 5000000, 50000000
What conclusion can you make about the C++ random number generator rand()?
What happens to the response time of your code?
Extra Credit Challenge: Validate the user input so that only values >= 0 are accepted for input. Then add a while loop that repeatedly prompts the user and generates the statistics until a 0 is entered. This is called a sentinel. Some possible example outputs:
Enter the number of values to be generated (0 to quit) : 5
Ones generated: 4 Percent: 80.00%
Twos generated: 1 Percent: 20.00%
WOW! Enter the number of values to be generated (0 to quit) : 500
Ones generated: 249 Percent: 49.80%
Twos generated: 251 Percent: 50.20%
WOW! Enter the number of values to be generated (0 to quit) : -15
Enter the number of values to be generated (0 to quit) : -30
Enter the number of values to be generated (0 to quit) : 50000
Ones generated: 24960 Percent: 49.92%
Twos generated: 25040 Percent: 50.08%
WOW! Enter the number of values to be generated (0 to quit) : 0
Goodbye! Press any key to continue . . .
Starting code:
int main() {
int noOfValues = 0; //user is prompted for this int value = 0; //randomly generated in the for loop. is either 1 or 2 int numberOfOnes = 0; //number of 1s generated int numberOfTwos = 0; //number or 2s generated
//prime the random number generator so it is ready to go srand(time(0));
cout << "Enter the number of values to generate : "; cin >> noOfValues;
//inside a "for" loop randomly generate a 1 or 2 and count the number of 1's and 2's //the number of times the loop iterates is determined by the number in noOfValues
//display the statistics. no changes needed cout << fixed << showpoint << setprecision(2); cout << "Ones generated: " << numberOfOnes << "\tPercent: "; cout << static_cast
cout << "Twos generated: " << numberOfTwos << "\tPercent: "; cout << static_cast
cout << "WOW! " << endl << endl;
system("pause");
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started