I need help for these 2 questions about C++. Thank you! 1. For Program 7, you we
ID: 3709806 • Letter: I
Question
I need help for these 2 questions about C++. Thank you!
1.
For Program 7, you were given the following code to generate a random value for a guessing game. You may have noticed that every time you ran the game, you got the same sequence of values to guess.
If I put just the random value generation in a loop in a small program:
for (int i = 0; i < 25; i++)
{
cout << rand() % (high - low + 1) + low << " ";
}
I get this output every time it runs.
Chapter 8 discusses this and shows a way to make sure that you generate different sequences of values when using rand().
Show the line of code that could be added to the guessing game program to make sure that different sequences of values are generated each time the program runs. (You do not have to add any include statements - assume that anything needed is present.)
2.
rand() returns a value that is between 0 and the constant value RAND_MAX. What is the lowest value that RAND_MAX will be (dependent on the compiler)?
Explanation / Answer
Here is the code..just add a line before for loop.
// This statement will create different sequence of
// random numbers on every program run this use current time as seed for random generator
srand(time(0));
for (int i = 0; i < 25; i++)
{
cout << rand() % (high - low + 1) + low << " ";
}
===============================================================
Kindly Check and Verify Thanks..!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.