Created
July 12, 2011 22:38
-
-
Save fabrizioc1/1079159 to your computer and use it in GitHub Desktop.
Generating a random password in one line
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
# 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