Created
December 15, 2016 19:57
-
-
Save elyscape/0e8b6ea511aa7d45aed4d0444775d20b to your computer and use it in GitHub Desktop.
Random generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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