Files
OpenBullet/RuriLib/Extensions.cs
T

33 lines
825 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RuriLib
{
/// <summary>
/// Extension methods for lists.
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Randomizes the elements in a list.
/// </summary>
/// <param name="list">The list to shuffle</param>
/// <param name="rng">The random number generator</param>
public static void Shuffle<T>(this IList<T> list, Random rng)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}