Question: Step 1: Copy this program into your C++program editor, and compile it. Hopefully you will not get any error messages. Step 2: Run the program

Step 1: Copy this program into your C++program editor, and compile it. Hopefully you will not get any error messages. Step 2: Run the program to see what it prints. You should see 100 random looking numbers between the values of 0 and 99 . Run the program again to see what happens. Do the values look different or are they the same? Step 3: Type in "man random" in your Linux window (or do a google search for "c++ random function") to see the manual page for the random function. You will see that the function uses a "non-linear additive feedback random number generator" to calculate the outputs. This means that it will will create the same sequence of "pseudo-random" numbers every time it is used in a program. Step 4: To test to see if the "random" function is generating random values, we can count how many times each value occurs to see if it is reasonable. Modify the program above to add an if statement to count the number of times the random number "value" occurs in the output sequence. Step 5: Compile and run your program. How many times did the value 42 occur? Edit your program and change count to 1000 . How many times did 42 occur this time? Now change count to 10000 and run the program. Hopefully the value of "found" has gone up by a factor of 10 each time you increased count. Step 6: You may have noticed that the random numbers always started with the same value. This may be too predictable for some applications. For example, a video game where the first bad guy appears at the same location every time you play. Type in "man srandom" in your Linux window (or do a google search for "c++ srandom function") to learn about the "srandom" function. What does it do? What does it return? What parameters does it expect? Step 7: Edit your program above to change count back to 1000, and then add the following lines above the for loop. Then replace the comment with the correct call to the srandom function based on what you learned from the manual page. int seed; cout "Enter seed: "; cin seed; // call srandom Step 8: Compile and run your program. Try a few different values for the seed and see what happens. You should see that the value 42 occurs roughly 10 times in 1000 random values, which is what we would expect. Congratulations, you are now ready to use random numbers to test future programs
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
