DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Random Password Generator For Ruby
Unfriendly / easily mistaken characters (i o 0 1 l 0) are excluded.
def random_password(size = 8)
chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
(1..size).collect{|a| chars[rand(chars.size)] }.join
end
puts random_password.inspect(July 14 - Fixed due to comment by friendly commenter below :))





Comments
Snippets Manager replied on Tue, 2009/01/06 - 8:48am
Snippets Manager replied on Wed, 2006/07/12 - 9:49pm
Snippets Manager replied on Mon, 2012/05/07 - 2:24pm
def random_pronouncable_password(size = 4) c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr) v = %w(a e i o u y) f, r = true, '' (size * 2).times do r << (f ? c[rand * c.size] : v[rand * v.size]) f = !f end r end puts random_pronouncable_password.inspect