jeudi 25 février 2016

C# Generate random string new instance returning same random string [duplicate]

This question already has an answer here:

I have a function to create a random string :

    public string RndStrings(int Groot)
    {
        Random randa = new Random();

        //The string that will contain all letters that are allowed to be generated.
        string totally = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_";
        //Make a string that will be returned;
        string Ro = "";
        //Add a random character from the string "Allowed" to the string "R".
        for (int i = 0; i < Groot; i++) Ro += totally[randa.Next(0, totally.Length)];
        //Return the random string.
        return Ro;
    }

When used like this without using a Thread.Sleep in between the results returned are the SAME.

        var random_pwd = new ClientTools();
        string random_string_pwd = random_pwd.RndStrings(15);
        var random_salt = new ClientTools();
        string random_string_salt = random_salt.RndStrings(15);

With a Thread.Sleep in between, they are different as I want. How is this possible because I create two new instances of ClientTools which contains the RndStrings function.

Thanks.




Aucun commentaire:

Enregistrer un commentaire