How to generate random password with specific characters in C#

1 Answer

0 votes
using System;
using System.Linq;
  
public class Program
{
    public static string GenerateSpecificRandomPassword(int length, string chars) {
        Random rnd = new Random();
  
        return new string(Enumerable.Repeat(chars, length)
                        .Select(s => s[rnd.Next(s.Length)]).ToArray());  
    }
  
    public static void Main()
    {
        int length = 5;
  
        string password = GenerateSpecificRandomPassword(length, "ABCDEFGhijkl!@#$&");
         
        Console.WriteLine(password);
    }
}
 
 
 
 
  
/*
run:

G@$DG
 
*/

 



answered Aug 17, 2023 by avibootz

Related questions

1 answer 144 views
3 answers 154 views
154 views asked Mar 28, 2023 by avibootz
1 answer 103 views
1 answer 105 views
1 answer 90 views
1 answer 91 views
3 answers 158 views
158 views asked Dec 22, 2024 by avibootz
...