Check api for SecureRandom: http://rails.rubyonrails.org/classes/ActiveSupport/SecureRandom.html
I believe that you are looking for another method: #random_number.
SecureRandom.random_number(a_big_number)
Since #hex returns a hexadecimal number, it would be unusual to ask for a random result containing only numeric characters.
For basic use cases, just use #rand.
rand(9999)
Edited by:
, , , . :
def rand_by_length(length)
rand((9.to_s * length).to_i).to_s.center(length, rand(9).to_s).to_i
end
#rand_by_length . String # center . #rand . .