Type a statement using srand() to seed random number generation using variable seedVal. Then type two statements using rand() to print two random integers between (and including) 0 and 9. End with a newline. Ex: #include < iostream > #include < cstdlib > // Enables use of rand() #include < ctime > // Enables use of time() using namespace std: int main() { int seedVal = 0 /* Your solution goes here */ return 0: }

Answer :

MrRoyal

Answer:

Replace /* Your solution goes here */ with the following code segment

srand(seedVal);

cout<<rand()%(int)(9 - 0 + 1) + 0<<endl;

cout<<rand()%(int)(9 - 0 + 1) + 0<<endl;

Explanation:

From the code segment, the seedVal has already been declared and initialized to 0; The next step is to seed it as random number using srand.

The first line of the code segment in the Answer section "srand(seedVal); " is used to achieve that task.

Having done that, the next step is to write statements to generate the two random numbers using rand();

When generating random numbers, two things are to be noted;

  • The lower bound; L
  • The upper bound; U

Here, the lower bound is 0 and the upper bound is 9;

C rand() function uses the following syntax to generate random numbers

rand()%(int)(U - L + 1) + L

Where U = 9 and L = 0

Replacing U and L with there respective values; the syntax becomes

rand()%(int)(9 - 0 + 1) + 0

So, the two print statements will be written as

cout<<rand()%(int)(9 - 0 + 1) + 0;

Also, the question states that, each print statement be ended with a new line;

So, the print statements will be

cout<<rand()%(int)(9 - 0 + 1) + 0<<endl;

Alternatively, by solving (9 - 0 + 1) + 0; the statements can be written as

cout<<rand()%(int)(10)<<endl;

Conclusively, the complete statements is as follows;

srand(seedVal);

cout<<rand()%(int)(9 - 0 + 1) + 0<<endl;

cout<<rand()%(int)(9 - 0 + 1) + 0<<endl;

or

srand(seedVal);

cout<<rand()%(int)(10)<<endl;

cout<<rand()%(int)(10)<<endl;

Other Questions