lundi 15 février 2021

Creating Random Strinngs in Ruby with at least one sepcial character, one digit, one upper-case, one lowercase with no characters repeated in Ruby

My task is to generate a random string with following parameters:

  1. At least one Uppercase
  2. At least one lower
  3. At least one digit
  4. No repeated chars/digits allowed ( e.g. aa not allowed, aba is allowed, Aa is allowed)

I'm able to generate a random string with 1,2,3 parameters but parameter 4 logic is missing.

inputChars = [('a'..'z'), ('A'..'Z'),(0..9)].map(&:to_a).flatten
string = (0...16).map { inputChars[rand(inputChars.length)] }.join

Aucun commentaire:

Enregistrer un commentaire