Ruby: Random

Standard Rand

rand #=> 0.13002209003779552

Rand with limit

SecureRandom

require 'securerandom'

SecureRandom.random_number #=> 0.232
SecureRandom.random_number 100 #=> 72
SecureRandom.hex #=> "be746d858318812801cfc043cf374ed9"

Sampling

Random String

def generate_code(number)
  charset = Array('A'..'Z') + Array('a'..'z')
  Array.new(number) { charset.sample }.join
end

puts generate_code(20)

S