338,898 views
27 votes
27 votes
Which statement, executed once at the start of main(), enables a program to generate a different sequence of pseudo-random numbers from rand() each time the program is run?

User Leafmeal
by
3.1k points

1 Answer

27 votes
27 votes

Answer:

(b) srand(time(0))

Step-by-step explanation:

Given

See attachment for options

Required

Which would generate different set of random numbers

To do this, we analyze each of the options - one at a time.

(a) srand()

The above is an invalid usage of srand() because srand() requires an argument for it to function e.g. srand(2)

(b) srand(time(0))

The above is valid, and it will return different pseudo random numbers each time the statement is executed because time(0) lets srand() seed to the current time.

(c) srand(100)

The above is a valid usage of srand(), however rand() will generate same set of numbers because srand() has been seeded to only 100.

(d) time(srand())

The above is also an invalid usage of srand().

Which statement, executed once at the start of main(), enables a program to generate-example-1
User Mounesh
by
2.9k points