2023-11-17 13:33:05 +01:00
|
|
|
|
namespace KIKunstKirstenKlöckner.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class ListExtension
|
|
|
|
|
{
|
|
|
|
|
public static IEnumerable<T> PickRandom<T>(this List<T> list, int n)
|
|
|
|
|
{
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
{
|
2024-06-30 13:38:37 +02:00
|
|
|
|
yield break;
|
2023-11-17 13:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
if (n <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("n must be greater than 0: ", nameof(n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
|
{
|
|
|
|
|
int index = Random.Shared.Next(list.Count);
|
|
|
|
|
yield return list[index];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|