Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random generators
#!/usr/bin/env ruby
require 'securerandom'
print 'Enter desired length of random hex string: '
length = gets
begin
actual_length = Integer(length)
byte_length = (actual_length / 2.0).ceil
raise unless byte_length > 0
rescue
puts 'Invalid length.'
exit 1
end
puts SecureRandom.hex(byte_length)[0, actual_length]
#!/usr/bin/env ruby
require 'securerandom'
print 'Enter desired number of digits: '
digits = gets
begin
length = Integer(digits)
raise unless length > 0
rescue
puts 'Invalid number of digits.'
exit 1
end
puts format "%0#{length}d", SecureRandom.random_number(10**length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.