I have got an IEnumerable collection as follows
var result1 = GetResult1() // Returns 2,4,5,6
I have to juggle the elements and create another collection in a random way which should result as follows:
var result2 = GetResult2(result1) // Returns 2,4,5,6 in a random order.
// An example output would be 4,6,2,5 in the resultant collection.
I have done this by the following means:
var result1 = GetResult1();
var random = new Random();
var result2 = result1.OrderBy(order=>random.Next());
However the issue with this is that if I access result2 the elements in result2 gets shuffled again, i.e if I output the results of result2 to a console twice, the elements are juggled again.
Can you please advice how to keep this uniform. i.e once I juggle the collection, it should remain the same way thereafter. I have to use lazy evaluation though, since the results are very huge in size.
I see you require lazy evaluation for the results, if that is the case, what you can do is this:
By calling
ToArray()on the random numbers, these will not change. When you finally desire the items inresult1, you can zip the items with the random numbers,OrderBythe random number andSelectthe result.As long as the items in
result1come in the same order, the result inorderedResultshould be the same each time.