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 110 views
3 answers 125 views
125 views asked Mar 28, 2023 by avibootz
1 answer 72 views
1 answer 67 views
1 answer 57 views
1 answer 62 views
3 answers 104 views
104 views asked Dec 22, 2024 by avibootz
...