Logo

Programming-Idioms

History of Idiom 69 > diff from v27 to v28

Edit summary for version 28 by steenslag:
[Ruby] added comment to about not seeding being better

Version 27

2018-06-11, 22:31:08

Version 28

2018-06-11, 22:50:25

Idiom #69 Seed random generator

Use seed s to initialize a random generator.

If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time.

Idiom #69 Seed random generator

Use seed s to initialize a random generator.

If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time.

Code
r = Random.new(s)
Code
r = Random.new(s)
Comments bubble
Don't use system time to seed; not specifying s is far superior. (Docs: If number is omitted, seeds the generator using a source of entropy provided by the operating system, if available (/dev/urandom on Unix systems or the RSA cryptographic provider on Windows), which is then combined with the time, the process id, and a sequence number.)
Doc URL
http://ruby-doc.org/core-2.2.3/Random.html#method-c-new
Doc URL
http://ruby-doc.org/core-2.2.3/Random.html#method-c-new