Last active
October 12, 2020 01:27
-
-
Save HassakuTb/40a8a46607166ac013254d2b5e03850c to your computer and use it in GitHub Desktop.
Fisher-Yatesシャッフル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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