Extension helpers for random selection and shuffling. Provides FromList<T> to pick a random element from an IReadOnlyList<T>, and Shuffle<T> to return a randomized array copy of an IEnumerable<T> using Fisher-Yates.
public static class RandomExtensions
{
public static T FromList<T>(this Random random, IReadOnlyList<T> list)
{
if (list.Count == 0)
{
return default;
}
var index = random.Next(list.Count);
return list[index];
}
public static IReadOnlyList<T> Shuffle<T>(this IEnumerable<T> items, Random random = null)
{
var copy = items.ToArray();
random ??= Random.Shared;
for (var i = 0; i < copy.Length - 1; ++i)
{
var j = random.Next(i, copy.Length);
(copy[i], copy[j]) = (copy[j], copy[i]);
}
return copy;
}
}