[[ググる]]と出てきた[[LINQ]]使ったやつ。
<source lang="csharp">
using System;
using System.Collections.Generic;
using System.Linq;
public static class IEnumerableExtension
{
</source>
乱数生成器が指定できたほうがよくね?Randomクラスはアルゴリズムがよろしくないらしいが、シールドクラスではないのでメルセンヌ・ツイスタあたりを実装すればいい。乱数生成器が指定できたほうがよくね?Randomクラスは[[アルゴリズム]]がよろしくないらしいが、シールドクラスではないのでメルセンヌ・ツイスタあたりを実装すればいい。
* [[C♯でメルセンヌ・ツイスタ乱数]]
<source lang="csharp">
using System; using System.Collections.Generic; using System.Linq; public static class IEnumerableExtensionsIEnumerableExtension
{
public static IEnumerable<TSourceT> Shuffle<TSourceT>(this IEnumerable<TSourceT> sourcecollection, Random random)
{
var copy = sourcereturn collection.ToListOrderBy(); int count = 0; while (0 < (count = copy.Count)) { int index i = > random.Next(count); yield return copy[index]; copy.RemoveAt(index); }
}