generate random number in ruby

Sometimes we would also need to generate x random numbers from n to m

x.times.map{n + Random.rand( m - n )}
Random.new.rand(n...m)
#in ruby 1.9.3
Random.rand(n...m)

if you only want to select a random item form array.

%(a b c d e).sample
(n...m).to_a.sample

Some other ways by using SecureRandom

require 'securerandom'

p SecureRandom.random_number(100) #=> 15
p SecureRandom.random_number(100) #=> 88

p SecureRandom.random_number #=> 0.596506046187744
p SecureRandom.random_number #=> 0.350621695741409

p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"