Random and SecureRandom
Random numbers are an important part of games and cryptographic security, and Ruby has a few different ways to get randomness. The easiest is the method Kernel#rand(max = 0). Because it’s available in Kernel, you can call rand anytime. If you call rand with no arguments, you get a pseudo-random float greater than or equal to zero and less than one. If you call rand with an argument and the argument is an integer one or greater, then you get a pseudo-random integer greater than or equal to zero and less than the argument. Note this means that rand(1) will always equal 0.
Non-integer arguments are converted using to_i.abs, meaning that negative arguments will return positive values and floating-point arguments will be ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access