Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CHALLENGE ACTIVITY 5.4.2: rand function: Seed and then get random numbers Type a statement using srand() to seed random number generation using variable seedVal. Then

image text in transcribedimage text in transcribed
CHALLENGE ACTIVITY 5.4.2: rand function: Seed and then get random numbers 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: J 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 1 #include WN #include // Enables use of rand() #include // Enables use of time() 1 test passed 4 using namespace std; 6 int main() { All tests int seedVal = 0; passed 9 * Your solution goes here * / 10 11 return 0; 12 3 RunPseudo-random The integers generated by randO are known as pseudo-random. "Pseudo" means "not actually, but having the appearance of". The integers are pseudo-random because each time a program runs, calls to randO yield the same sequence of values. Earlier in this section, a program called rand0 three times and output 16807, 282475249, 1622650073. Every time the program is run, those same three integers will be printed. Such reproducibility is important for testing some programs. (Players of classic arcade games like Pac-man may notice that the seemingly-random actions of objects actually follow the same pattern every time the game is played, allowing players to master the game by repeating the same winning actions). Internally, the rand() function has an equation to compute the next "random" integer from the previous one, (invisibly) keeping track of the previous one. For the rst call to randO, no previous random integer exists, so the function uses a built-in integer known as the seed. By default, the seed is 1. A programmer can change the seed using the function srandO, as in srand(2) or srand(99). If the seed is different for each program run, the program will get a unique sequence. One way to get a different seed for each program run is to use the current time as the seed. The function timeo returns the number of seconds since Jan 1, 1970. Note that the seeding should only be done once in a program, before the first call to rand(). Figure 5.4.3 : Using a unique seed for each program run. 636952311 51510682 304122633 (next run) 637053153 1746362176 1450088483

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linear Algebra A Modern Introduction

Authors: David Poole

4th edition

1285463242, 978-1285982830, 1285982835, 978-1285463247

More Books

Students also viewed these Mathematics questions