Skip to content

Instantly share code, notes, and snippets.

@fabrizioc1
Created July 12, 2011 22:38
Show Gist options
  • Save fabrizioc1/1079159 to your computer and use it in GitHub Desktop.
Save fabrizioc1/1079159 to your computer and use it in GitHub Desktop.
Generating a random password in one line
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment