Alex Rivera | Logout

Is using Random and OrderBy a good shuffle algorithm?

Asked 2009-08-17T12:00:11.837
182

I have read an article about various shuffle algorithms over at Coding Horror. I have seen that somewhere people have done this to shuffle a list:

var r = new Random();
var shuffled = ordered.OrderBy(x => r.Next());

Is this a good shuffle algorithm? How does it work exactly? Is it an acceptable way of doing this?

Edit
Report

1 Answer

1

This algorithm shuffles by generating a new random value for each value in a list, then ordering the list by those random values. Think of it as adding a new column to an in-memory table, then filling it with GUIDs, then sorting by that column. Looks like an efficient way to me (especially with the lambda sugar!)

answered 2009-08-17T12:03:05.340

Your Answer