tlizcg
Last Updated: February 25, 2016
·
1.321K
· chadmoran
Cf8a7061176faf12195fdfbcdcbdd69b

Order by Random in C#

AnyEnumerable.OrderBy(_ => Guid.NewGuid())
Say Thanks
Respond

4 Responses
Add your response

1934
25d3faf538ec9842dafa7a11162fad77

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
Cf8a7061176faf12195fdfbcdcbdd69b

@ppiotrowicz Fixed! Thanks.

over 1 year ago ·
3459
522e570802b242cc65b97253797e65b4

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
522e570802b242cc65b97253797e65b4

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 ·