Ruby Randomness
Ever since reading Nassim Taleb’s Fooled By Randomness, I have tried to be wary of my day-to-day dealings with probability. Over the past few weeks, I have come across Ruby’s rand method several times and thought it would be useful to explore it below.
Ruby’s Rand
The Ruby rand documentation is as follows:
rand(max=0) → number
If called without an argument, or if max.to_i.abs == 0, rand returns a pseudo-random floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.
As stated above, rand can be called without an argument:
Next, if rand is called with an integer argument, it will return an integer greater than or equal to 0 but less than the argument:
If rand is called with a positive floating point number argument, rand will return an integer greater than or equal to 0, but less than the closest integer that is less than or equal to the argument (Ruby forces this on the argument using .to_i):
Users can also use rand on ranges of numbers. For example, rand(x..y) will return an integer between x and y (including both x and y). If y needs to be excluded, rand(x…y) can be utilized instead.
These applications of rand could be useful when the user needs to quickly simulate games of chance (think dice rolls) or perhaps generate a random quotation from a curated list.
Ruby also provides users with the access to a Random class, and you can use this to handle floating point numbers.
Here, you can call prng.rand(x) on a floating point number to return a floating point number that is between 0.0 and x (excluding x).
The rand method continues to work on ranges here and can return floating point numbers.
Why Ruby’s Built-in Rand Can Be Problematic
Since rand allows users to access Ruby’s pseudo-random number generator, the method has some subtleties that the user should keep in mind. In general, pseudo-random number generators use algorithms to produce sequences of numbers that appear random. The sequences, however, are actually determined by an initial parameter termed a key (or seed). Additionally, given a large amount of time, a particular sequence will eventually repeat itself. This determinism and periodicity do not make these generators ideal in situations that necessitate strict cryptographic security.
An Alternative Method to Generating Random Numbers in Ruby
Instead of monitoring the radioactive decay of some element within the controlled lab in your bedroom, you can use the RealRand gem. This gem allows ruby users to request random numbers from several public sites that purport to host true random number generators. The sources for these generators mostly include natural phenomenon such as atmospheric noise and radioactive decay.
Sources
Taleb, Nassim Nicholas. FOOLED BY RANDOMNESS. RANDOM HOUSE, INC, 2008.