vendredi 28 janvier 2022

How do I generate a random string of 8 distinct characters in c# [closed]

I can generate a random string of 8 character with this

public static Random random = new Random();

    public static string GenerateString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return new string(Enumerable.Repeat(chars, length)
            .Select(s => s[random.Next(s.Length)]).ToArray()).ToLower();
    }`

However the chars are not distinct. Any help on how can I make it distinct and generate still a string 8 chars long? I tried with distinct but then length gets affected as deletes the duplicate without taking into account the length asked.




Aucun commentaire:

Enregistrer un commentaire