Last Updated: February 25, 2016
·
8.709K
· chadmoran

Order by Random in C#

AnyEnumerable.OrderBy(_ => Guid.NewGuid())

4 Responses
Add your response

Hi, this is nice. Didn't thought about using Guid for randomizing enumerables. Clever!
But you have a typo in it.

over 1 year ago ·

@ppiotrowicz Fixed! Thanks.

over 1 year ago ·

This is much better: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

There's a discussion about it on SO: stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm/1287572

over 1 year ago ·

Anyways, I prefer my implementation because it catches argument errors earlier.

Here it is: https://bitbucket.org/jpbochi/jplabscode/src/025d97301e69600f046c65281a009733be5deed9/Extensions/EnumerableExt.cs?at=default#cl-309

over 1 year ago ·