lundi 2 novembre 2015

Random not working inside LINQ [duplicate]

This question already has an answer here:

Consider the following code:

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
IEnumerable<string> randoms = Enumerable.Range(1, 10)
                              .Select(i => 
                              new string(alphabet
                                         .OrderBy(c => new Random().Next())
                                         .ToArray()));

As you can see I am ordering by new Random().Next() which should be a different number in each evaluation, right?

However this is the result I am getting:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

As you can see none of the characters got shuffled.

What is happening here?




Aucun commentaire:

Enregistrer un commentaire