Question
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 getting a random number. A value of 1 (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(1);
To get a different series of random numbers each time the program is run, the actual time that the program is run can be passed as the seed value for the random number generator. This is done as follows:
srand(time(0));
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 0 and RAND_MAX and saves the value in an integer variable named num. RAND_MAX is a pre-defined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least 32,767.
Modulus division can be used to restrict the "random" integer to a smaller range.
To generate a value between 0 and 7: num = rand() % 8;
To change the range to 1 through 8, simply add 1: num = rand() % 8 + 1;
To get random values that are within a specified range that starts at a value other than 0 or 1: num = minimum_value + (rand() % (maximum_value - minimum_value + 1));
So, to get values within the range 6 - 22: num = 6 + (rand() % (22 - 6 + 1));
To convert a random integer value to a random double value: double_num = minimum_value + (rand() / (RAND_MAX / (maximum_value - minimum_value))); where minimum_value and maximum_value are both double values.
So, to get values within the range 0.5 - 230.5: double_num = 0.5 + (rand() / (RAND_MAX / (230.5 - 0.5)));
Basic Program Logic
Initialize the random number generator using a seed value of 12. 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 12.
The first set of numbers should have exactly 30 values. Display the number of values in the first set with a label.
In a for loop that executes exactly thirty times:
generate a random number (no restrictions)
display the random number *
* when displaying the random numbers, make sure that there are exactly 7 values displayed per line. The exception is the last line, which may have less than 7 values displayed.
Next, generate a random number between 1 and 75. 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 7 values displayed per line.
Finally, generate a random number between 1 and 155. 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 0.0 and 100.0
display the random number with 5 digits after the decimal point ***
*** again, make sure that there are exactly 7 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 6 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 30.
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 75.
The third constant represents the maximum size of the third set of values (the one generated by the do while loop). It should have an integer value of 155.
The fourth constant represents the minimum random double value. It should have a value of 0 (make sure to use 0.0 if using #define to create the constant).
The fifth constant represents the maximum random double value. It should have a value of 100 (make sure to use 100.0 if using #define to create the constant).
The sixth constant represents the number of values to display on a line of output. It should have an integer value of 7.
More symbolic constants may be added to the code if necessary
Program Requirements
1. Include line documentation. There is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describes what the "chunk" of code does. This will also be a part of every program that is submitted for the remainder of the semester.
2. To use the random number generator, add #include at the beginning of the program
3. The program MUST use the 6 symbolic constants described above. Make sure to follow the programming convention of capitalizing the names of constants.
4. Make sure that the copy of the program that is handed in uses srand(12); to set the seed value for the random number generator.
5. The numbers in each set MUST be displayed in columns with the LAST digit of the values lined up.
6. Hand in a copy of the source code (the CPP file) using Blackboard.
Output
Run 1 (using srand(12);) on Windows PC
Set 1 -- 30 values
77 5628 6232 29052 1558 26150 12947 29926 11981 22371 4078 28629 4665 2229 24699 27370 3081 18012 24965 2064 26890 21054 5225 11777 29853 2956 22439 3341 31337 14755
Set 2 -- 65 values 24855 4173 32304 292 5344 15512 12952 1868 10888 19581 13463 32652 3409 28353 26151 14598 12455 26295 25763 26040 8285 27502 15148 4945 26170 1833 5196 9794 26804 2831 11993 2839 9979 27428 6684 4616 30265 5752 32051 10443 9240 8095 28084 26285 8838 18784 6547 7905 8373 19377 18502 27928 13669 25828 30502 28754 32357 2843 5401 10227 22871 20993 8558 10009 6581
Set 3 -- 87 values 39.08811 14.20026 75.05417 65.71551 28.70876 20.87466 92.68166 7.11081 0.00916 85.52507 67.95251 58.98312 55.28123 55.23850 47.74316 66.31062 52.77261 25.62334 84.13038 6.10981 11.68859 38.34346 4.33363 90.12421 66.59139 30.37812 25.38835 33.32011 24.34767 75.70421 91.63793 53.89264 26.74642 9.94293 63.23130 20.26124 71.88940 78.69503 33.71685 10.81576 97.50053 0.06714 4.85549 1.96539 79.29014 82.14972 96.79250 50.13276 47.45933 85.93097 21.68950 83.30638 74.52010 74.41633 98.99594 67.82434 37.49199 38.45637 40.39125 65.93829 7.67846 39.96399 82.64412 49.83978 71.09287 63.16111 96.37745 87.76513 32.64565 14.43525 48.99747 67.77551 7.29698 61.47343 49.82147 74.88327 51.20396 52.24464 55.53758 87.09067 36.05152 4.54726 64.19263 6.03656
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