vendredi 26 mai 2017

C# cannot generate a random string [duplicate]

I cannot work out why C# is doing this.

Here's my code;

private string RandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string randomString = "";

        for(int i = 0; i < length; i++)
        {
            randomString += chars.ToCharArray()[new Random().Next(chars.ToCharArray().Length)];
        }

        return randomString;
    }

First result: "wwwwwwwwwwwwwwwwwwww"

Second result: "ssssssssssssssssssss"

Third result: "mmmmmmmmmmmmmmmmmmmm"




Aucun commentaire:

Enregistrer un commentaire