Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate random password-ish string in Ruby.
#!/usr/bin/env ruby
# numbers
def generate_random_numbers(length)
chars = (0..9).to_a.map(&:to_s)
length.times.map{ chars.shuffle.first }.join
end
# lowercase letters
def generate_random_lowercase_letters(length)
chars = ('a'..'z').to_a
length.times.map{ chars.shuffle.first }.join
end
# uppercase letters
def generate_random_uppercase_letters(length)
chars = ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# symbols
def generate_random_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - (0..9).to_a.map(&:to_s) - ('a'..'z').to_a - ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + lowercase letters
def generate_random_numbers_and_lowercase_letters(length)
chars = (0..9).to_a.map(&:to_s) + ('a'..'z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + uppercase letters
def generate_random_numbers_and_uppercase_letters(length)
chars = (0..9).to_a.map(&:to_s) + ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + symbols
def generate_random_numbers_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - ('a'..'z').to_a - ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# lowercase letters + uppercase letters
def generate_random_letters(length)
chars = ('a'..'z').to_a + ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# lowercase letters + symbols
def generate_random_lowercase_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - (0..9).to_a.map(&:to_s) - ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# uppercase letters + symbols
def generate_random_uppercase_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - (0..9).to_a.map(&:to_s) - ('a'..'z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + lowercase letters + uppercase letters
def generate_random_numbers_and_letters(length)
chars = (0..9).to_a.map(&:to_s) + ('a'..'z').to_a + ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + lowercase letters + symbols
def generate_random_numbers_and_lowercase_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - ('A'..'Z').to_a
length.times.map{ chars.shuffle.first }.join
end
# numbers + uppercase letters + symbols
def generate_random_numbers_and_uppercase_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - ('a'..'z').to_a
length.times.map{ chars.shuffle.first }.join
end
# lowercase letters + uppercase letters + symbols
def generate_random_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) } - (0..9).to_a.map(&:to_s)
length.times.map{ chars.shuffle.first }.join
end
# numbers + lowercase letters + uppercase letters + symbols
def generate_random_numbers_and_letters_and_symbols(length)
chars = 0.upto(127).map(&:chr).select{|c| c.match?(/[[:print:]]/) }
length.times.map{ chars.shuffle.first }.join
end
generate_random_numbers(10) # => "3490808694"
generate_random_lowercase_letters(10) # => "pasmjlhksf"
generate_random_uppercase_letters(10) # => "WUIQMBVWME"
generate_random_symbols(10) # => "[(=?!*;:& "
generate_random_numbers_and_lowercase_letters(10) # => "n2ccmeuq0w"
generate_random_numbers_and_uppercase_letters(10) # => "DI7LT07APH"
generate_random_numbers_and_symbols(10) # => ")&0^(_0@}8"
generate_random_letters(10) # => "iTHyWvehVp"
generate_random_lowercase_letters_and_symbols(10) # => "f]:'xo}opp"
generate_random_uppercase_letters_and_symbols(10) # => "DQ\\M\"V^PK."
generate_random_numbers_and_letters(10) # => "QiZYdkftIw"
generate_random_numbers_and_lowercase_letters_and_symbols(10) # => "9p@%;^>14,"
generate_random_numbers_and_uppercase_letters_and_symbols(10) # => "T|`ZLQ(;*2"
generate_random_letters_and_symbols(10) # => "=r!RSpDtPm"
generate_random_numbers_and_letters_and_symbols(10) # => "qv6Wp-fVMb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment