Generate a Random Secure Password with Ruby

You may use the following Ruby script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

Generate a Random Secure Password with Ruby
Photo by Tianyi Ma / Unsplash

You may use the following Ruby script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

# Define an array of characters to use in the password
characters = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten + ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|', '\\', ';', ':', ',', '.', '<', '>', '/', '?']

# Define the desired length of the password
length = 15

# Generate a random password using the defined characters and length
password = (0..length).map { characters.sample }.join

puts "#{password}"

By default the length of the password is 15 characters. You may change the length of the password on line 7.