mardi 23 août 2016

How to randomize seed in C# [duplicate]

This question already has an answer here:

I need to generate random int in C#. I am using clock time to set the seend. However, as the rnd.Next() function may take less than a millisecond, this does not work if one has to generate a list of ints.

        for( int i=0; i<5; i++) {
            int max_val = 10; // max value
            var rnd = new Random(DateTime.Now.Millisecond);
            int randind = rnd.Next(0, max_val);
            Console.WriteLine(randind);
        }

Output:
1 5 5 5 5

How can one randomise the seed in a clean way without adding an ugly sleep?




Aucun commentaire:

Enregistrer un commentaire