Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Overview For this assignment, write a program that will generate three sets of random numbers. Random Number Generation In the first three programs, the user
Overview
For this assignment, write a program that will generate three sets of random numbers.
Random Number Generation
In the first three programs, the user was asked for input. This program will be different. Rather than asking the user how many values are in a set of numbers or even what the values are, a random number generator will be used to determine the size of a set and the actual values in the set.
To use the random number generator, first add a #include statement for the cstdlib library to the top of the program:
#include
Next, initialize the random number generator. This is done by calling the srand function and passing it an integer value known as a seed value This should only be done ONE time and it must be done BEFORE actually generating a random number. A value of or any integer literal will generate the same sequence of "random" numbers every time the program is executed. This can be useful for debugging:
srand;
To get a different series of random numbers each time the program is run, the time that the program is run can be passed as the seed value for the random number generator. This is done as follows:
srandtime;
If the time function is used, make sure to #include the ctime library as well.
Note: the two srand instructions that are listed above are simple examples of how to use the instruction. In a program, ONLY ONE version will be used.
Now that the random number generator has been initialized, a random number can be generated by calling the rand function:
num rand;
The above line of C code will generate a "random" integer between and RANDMAX and saves the value in an integer variable named num. RANDMAX is a predefined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least
Modulus division can be used to restrict the "random" integer to a smaller range.
To generate a value between and : num rand;
To change the range to through simply add : num rand;
To get random values that are within a specified range that starts at a value other than or : num minimumvalue randmaximumvalue minimumvalue ;
So to get values within the range : num rand;
To convert a random integer value to a random double value: doublenum minimumvalue randRANDMAX maximumvalue minimumvalue;
where minimumvalue and maximumvalue are both double values. So to get values within the range : doublenum randRANDMAX ;
Basic Program Logic
Initialize the random number generator using a seed value of Other seed values may be used to produce different results. However, the version that is handed in for grading MUST use a seed value of
The first set of numbers should have exactly values. Display the number of values in the first set with a label.
In a for loop that executes exactly fortyfive times:
generate a random number no restrictions
display the random number
when displaying the random numbers, make sure that there are exactly values displayed per line. The exception is the last line, which may have less than values displayed.
Next, generate a random number between and This will be the number of values in the second set. Display the number of values in the set with a label.
In a while loop that executes exactly "number of values in the second set" number of times:
generate a random number no restrictions
display the random number
again, make sure that there are exactly values displayed per line.
Finally, generate a random number between and This will be the number of values in the third set. Display the number of values in the set with a label.
In a do while loop that executes exactly "number of values in the third set" number of times:
generate a random double number between and
display the random number with digit after the decimal point
again, make sure that there are exactly values displayed per line.
Note: when writing this program, it is important that the steps that involve the random number generator are executed in the sequence that they're listed above. This is because the random number generator simply generates a sequence of values and if those values are not processed in the same order as above, the results will not match the expected results that are listed in the Output section below.
Symbolic Constants
This program MUST use at least symbolic constants.
The first constant represents the size of the first set of values the one generated by the for loop It should have an integer value of
The second constant represents the maximum size of the second set of values the one generated by the while loop It should have an integer value of
The
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