Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Last active October 12, 2020 01:27
Show Gist options
  • Save HassakuTb/40a8a46607166ac013254d2b5e03850c to your computer and use it in GitHub Desktop.
Save HassakuTb/40a8a46607166ac013254d2b5e03850c to your computer and use it in GitHub Desktop.
Fisher-Yatesシャッフル
using System.Collections.Generic;
using System.Linq;
public static partial class IListExtensions {
/// <summary>
/// Fisher-Yatesシャッフル の実装
/// </summary>
public static IList<T> Shuffle<T>(this IList<T> list, RandomGenerator rgen) {
IList<T> target = list.ToList();
for (int i = target.Count; i > 1; --i) {
int a = i - 1;
int b = rgen.Range(0, i);
T tmp = target[a];
target[a] = target[b];
target[b] = tmp;
}
return target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment