vendredi 13 décembre 2019

How can I create a random string with a certain number of numbers

I would like to create a function that allows me to generate a random string with certain conditions like maximum 2 digits, etc.

So far I've found this but I don't know how to make it two digits maximum:

public static string Generate(int size)
        {
            const string chars = "abcdefghijklmnopqrstuvwxzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var random = new Random();
            return new string(Enumerable.Repeat(chars, size).Select(s => s[random.Next(s.Length)]).ToArray());
        }



Aucun commentaire:

Enregistrer un commentaire