tlizcg
Last Updated: February 25, 2016
·
2.5K
· chadmoran

Order by Random in C#

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

4 Responses
Add your response

1934

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 ·
1935

@ppiotrowicz Fixed! Thanks.

over 1 year ago ·
3459

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 ·
3460

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 ·