Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 88d349d

Browse files
committed
Add extension methods for shuffling arrays and lists
1 parent 047ebb6 commit 88d349d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Runtime/Extensions/ArrayExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,45 @@ public static T[] Reversed<T>(this T[] array)
578578
return reversed;
579579
}
580580

581+
/// <summary>
582+
/// Shuffles the array in place.
583+
/// </summary>
584+
/// <remarks>The shuffle is done using the Fisher-Yates algorithm.</remarks>
585+
/// <param name="array">The array to shuffle.</param>
586+
/// <typeparam name="T">The type of the array.</typeparam>
587+
public static void Shuffle<T>(this T[] array)
588+
{
589+
int n = array.Length;
590+
591+
while (n > 1)
592+
{
593+
int k = UnityEngine.Random.Range(0, n--);
594+
T temp = array[n];
595+
array[n] = array[k];
596+
array[k] = temp;
597+
}
598+
}
599+
600+
/// <summary>
601+
/// Shuffles the array in place using the given random number generator.
602+
/// </summary>
603+
/// <remarks>The shuffle is done using the Fisher-Yates algorithm.</remarks>
604+
/// <param name="array">The array to shuffle.</param>
605+
/// <param name="rng">The random number generator to use.</param>
606+
/// <typeparam name="T">The type of the array.</typeparam>
607+
public static void Shuffle<T>(this T[] array, Random rng)
608+
{
609+
int n = array.Length;
610+
611+
while (n > 1)
612+
{
613+
int k = rng.Next(n--);
614+
T temp = array[n];
615+
array[n] = array[k];
616+
array[k] = temp;
617+
}
618+
}
619+
581620
/// <summary>
582621
/// Returns a portion of the array containing the specified
583622
/// <paramref name="amount"/> of elements.

Runtime/Extensions/ListExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,45 @@ public static List<T> Reversed<T>(this List<T> list)
424424
return reversed;
425425
}
426426

427+
/// <summary>
428+
/// Shuffles the list in place.
429+
/// </summary>
430+
/// <remarks>The shuffle is done using the Fisher-Yates algorithm.</remarks>
431+
/// <param name="list">The list to shuffle.</param>
432+
/// <typeparam name="T">The type of the list.</typeparam>
433+
public static void Shuffle<T>(this List<T> list)
434+
{
435+
int n = list.Count;
436+
437+
while (n > 1)
438+
{
439+
int k = UnityEngine.Random.Range(0, n--);
440+
T temp = list[n];
441+
list[n] = list[k];
442+
list[k] = temp;
443+
}
444+
}
445+
446+
/// <summary>
447+
/// Shuffles the list in place using the given random number generator.
448+
/// </summary>
449+
/// <remarks>The shuffle is done using the Fisher-Yates algorithm.</remarks>
450+
/// <param name="list">The list to shuffle.</param>
451+
/// <param name="rng">The random number generator to use.</param>
452+
/// <typeparam name="T">The type of the list.</typeparam>
453+
public static void Shuffle<T>(this List<T> list, Random rng)
454+
{
455+
int n = list.Count;
456+
457+
while (n > 1)
458+
{
459+
int k = rng.Next(n--);
460+
T temp = list[n];
461+
list[n] = list[k];
462+
list[k] = temp;
463+
}
464+
}
465+
427466
/// <summary>
428467
/// Filters the list to only contain items that satisfy the
429468
/// <paramref name="predicate"/>.

0 commit comments

Comments
 (0)