+ 1

What actually srand() will do

22nd Jan 2017, 5:43 AM
Shalini Dubey
Shalini Dubey - avatar
2 Antworten
+ 3
srand() allows you to seed the starting point the range generated by rand(). srand() is usually called with time() as an argument which gives you a relatively random starting point .
22nd Jan 2017, 6:47 AM
mohammad Shojaeinia
mohammad Shojaeinia - avatar
+ 3
rand() doesn't produce a random number - it uses some quite easy formula to compute the next "random value" based on its stored internal state that changes each time a random value is generated. srand() sets that internal state. This way you can get reproduceable sets of numbers - you call srand() with a given value and rand() then produces a set of values. When you start the program next time and call srand() with exact same value rand() will produce exactly the same set of values. This is useful for simulation. Calling srand( time( NULL ) ) makes your program generate a set of values that will depend on the current time and therefore be irreproduceable - each time you restart a program a new set of numbers is generated.
22nd Jan 2017, 8:20 AM
Sampreet Gaonkar