mirror of
https://github.com/D1GQ/BetterAmongUs.git
synced 2026-10-20 01:46:19 +00:00
25 lines
786 B
C#
25 lines
786 B
C#
namespace BetterAmongUs.Helpers;
|
|
|
|
internal static class ShuffleListExtension
|
|
{
|
|
/// <summary>
|
|
/// Shuffles all elements in a collection randomly
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the collection</typeparam>
|
|
/// <param name="collection">The collection to be shuffled</param>
|
|
/// <param name="random">An instance of a randomizer algorithm</param>
|
|
/// <returns>The shuffled collection</returns>
|
|
internal static IEnumerable<T> Shuffle<T>(this IEnumerable<T> collection, Random random)
|
|
{
|
|
var list = collection.ToList();
|
|
int n = list.Count;
|
|
while (n > 1)
|
|
{
|
|
n--;
|
|
int k = random.Next(n + 1);
|
|
(list[n], list[k]) = (list[k], list[n]);
|
|
}
|
|
return list;
|
|
}
|
|
}
|