samedi 5 mars 2016

customised random string generator in ruby

Below is my code to generate a random string by taking the number of length from the user . I want to enhance this by asking the user to enter number of numbers and special characters to be included and generate a string to match those requirements.

For example:

length: 7
number of numbers: 2
number of special chars: 1

Then my output should be something like:

ab2hg3! 

Below is my code:

puts "enter the minimum length of the password"
lengths = gets.to_i
puts "enter the number of numbers to be included"
num = gets.to_i

def gen_pass(lengths)
    chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..10).to_a
    Array.new(lengths) {chars.sample}.join
end

puts gen_pass(lengths)
puts gen_pass(lengths)




Aucun commentaire:

Enregistrer un commentaire