Written by Sean Behan on Sun Jun 17th 2012

I assumed that rand would take a range as an argument. Something like rand(10..20), generating a random number between 10 and 20. Seems like you'd do this fairly often when working with random numbers and therefore, included. However, it doesn't work. But the solution is almost as easy.

10 + rand(11) #=> produces a random number between 10 and 20

Since rand starts at 0 (like array indexes), we need to add an extra 1 to get what we're expecting.


Tagged with..
#numbers #one #rand #ruby #simple #zero #ruby

Just finishing up brewing up some fresh ground comments...