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: 5 7 Note: For this activity, using one statement may yield different output (due to the compiler calling rand() in a different order). Use two statements for this activity. Also, after calling srand() once, do not call srand() again. (Notes)

Respuesta :

Answer:

View Image

Explanation:

No number is really ever random. It all has a seed, which is an initial value for the 'random' number to be generated. The rand() function uses an equation to  generate its value, and all its 'random' value depend on the first number you plug in into your equation. The initial number you plug into your equation is what srand() do.

If you just call rand() alone without calling srand() then the generated numbers will automatically use srand(1) as a default seed. Using the same srand() value will always yield the same sequence of numbers.

To get a number between 0 and 9, take the modulus of it with 10 because modulus give you the remainder, so the number rand()%10 will never be equal to 10 or greater.

Ver imagen GrandNecro
Ver imagen GrandNecro

rand(), srand() and seeding of values are all illustrations of generating random numbers in C++.

The first statement using srand() is:

  • srand(seedVal);

The other two statements to generate and print random integers between 0 and 9 are:

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

The first statement

The syntax of the srand() instruction is: srand(variable);

From the question, the seeding variable is seedVal.

So, the statement that implements the srand() instruction will be:

srand(seedVal);

The other two statements

The other statements are to generate and print random numbers between 0 and 9.

The syntax to generate random numbers between intervals in C++ is:

[tex]lower + rand() \% (upper - lower + 1);[/tex]

From the question;

[tex]lower = 0\\ upper = 9[/tex]

So, we have:

[tex]0 + rand() \% (9 - 0+ 1)[/tex]

This becomes

[tex]rand() \% (10)[/tex]

The above statement represented the random number that has been generated.

Next, is to print the numbers (this is done using cout statement).

So, the two statements to print the random numbers are:

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

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

Read more about random numbers at:

https://brainly.com/question/16792424

ACCESS MORE