Files
BetterAmongUs/src/Helpers/ShuffleHelper.cs
T
2026-01-09 00:21:34 -06:00

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;
}
}