Skip to content

Instantly share code, notes, and snippets.

@elyscape
Created December 15, 2016 19:57
Show Gist options
  • Save elyscape/0e8b6ea511aa7d45aed4d0444775d20b to your computer and use it in GitHub Desktop.
Save elyscape/0e8b6ea511aa7d45aed4d0444775d20b to your computer and use it in GitHub Desktop.
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