public
Created

Generating a random password in one line

  • Download Gist
random_password.rb
Ruby
1 2 3 4 5 6 7
# Ruby 1.9.2
password="".tap{|s| 8.times { s << sprintf("%c",Random.new.rand(33..126)) } }
# Another way
password=(33..126).to_a.shuffle.take(8).map(&:chr).join
# Using only safe/printable characters
valid_chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
password = valid_chars.shuffle.take(8).join

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.