Skip to content

Commit

Permalink
Update ListExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
adammyhre authored Apr 30, 2024
1 parent 9ce442e commit 3fc9c49
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions UnityUtils/Scripts/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ public static void Swap<T>(this IList<T> list, int indexA, int indexB) {
}

/// <summary>
/// Shuffles the elements in the list using the Fisher-Yates algorithm.
/// This method modifies the input list in-place.
/// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
/// Shuffles the elements in the list using the Durstenfeld implementation of the Fisher-Yates algorithm.
/// This method modifies the input list in-place, ensuring each permutation is equally likely, and returns the list for method chaining.
/// Reference: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
/// </summary>
/// <param name="list">The list to be shuffled.</param>
/// <typeparam name="T">The type of the elements in the list.</typeparam>
public static void Shuffle<T>(this IList<T> list) {
/// <returns>The shuffled list.</returns>
public static IList<T> Shuffle<T>(this IList<T> list) {
if (rng == null) rng = new Random();
int count = list.Count;
while (count > 1) {
--count;
int index = rng.Next(count + 1);
(list[index], list[count]) = (list[count], list[index]);
}
}
return list;
}
}
}

0 comments on commit 3fc9c49

Please sign in to comment.