-1
下面是我的代碼,通過從用戶獲取長度的數字來生成隨機字符串。我想通過要求用戶輸入要包含的數字和特殊字符的數量並生成符合這些要求的字符串來增強此功能。ruby中定製的隨機字符串生成器
例如:
length: 7
number of numbers: 2
number of special chars: 1
然後我的輸出應該是這樣的:
ab2hg3!
下面是我的代碼:
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)
感謝您的幫助。它的工作:)我有一個想法的方法來使用。謝謝 :) –