2023-06-21 08:06:54 +00:00
|
|
|
|
namespace Utils
|
|
|
|
|
{
|
|
|
|
|
public static class RandomUtils
|
|
|
|
|
{
|
|
|
|
|
private static readonly Random random = new Random();
|
2024-09-30 13:23:14 +00:00
|
|
|
|
private static readonly object @lock = new object();
|
2023-06-21 08:06:54 +00:00
|
|
|
|
|
|
|
|
|
public static T PickOneRandom<T>(this List<T> remainingItems)
|
|
|
|
|
{
|
2024-09-30 13:23:14 +00:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
var i = random.Next(0, remainingItems.Count);
|
|
|
|
|
var result = remainingItems[i];
|
|
|
|
|
remainingItems.RemoveAt(i);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-06-21 08:06:54 +00:00
|
|
|
|
}
|
2024-08-20 09:44:15 +00:00
|
|
|
|
|
|
|
|
|
public static T[] Shuffled<T>(T[] items)
|
|
|
|
|
{
|
2024-09-30 13:23:14 +00:00
|
|
|
|
lock (@lock)
|
2024-08-20 09:44:15 +00:00
|
|
|
|
{
|
2024-09-30 13:23:14 +00:00
|
|
|
|
var result = new List<T>();
|
|
|
|
|
var source = items.ToList();
|
|
|
|
|
while (source.Any())
|
|
|
|
|
{
|
|
|
|
|
result.Add(RandomUtils.PickOneRandom(source));
|
|
|
|
|
}
|
|
|
|
|
return result.ToArray();
|
2024-08-20 09:44:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-21 08:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|