Created
August 5, 2014 16:53
-
-
Save chris3000/22a355cdb0fd2fe356de to your computer and use it in GitHub Desktop.
Generates random password in easy-to-remember combinations
This file contains hidden or 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/ruby | |
#set amount of letters and numbers in passcode | |
num_len = 4 | |
alpha_len = 4 | |
#fill up alphabet array | |
a=[] | |
("a".."z").each {|v| a<<v} | |
#fill up number array | |
n=[] | |
("0".."9").each {|v| n<<v} | |
#generate passcode | |
code = [] | |
if Random.new.rand >= 0.5 #letters then numbers | |
alpha_len.times { code << a[(Random.new.rand*a.size).to_i] } | |
num_len.times { code << n[(Random.new.rand*n.size).to_i] } | |
else #numbers then letters | |
num_len.times { code << n[(Random.new.rand*n.size).to_i] } | |
alpha_len.times { code << a[(Random.new.rand*a.size).to_i] } | |
end | |
puts code.join("") | |
total_codes = ((a.size**alpha_len)*(n.size**num_len)*2) | |
puts "total possible codes: #{total_codes}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment