adding shuffle
This commit is contained in:
21
Kasbot.APP/Extensions/IListExtensions.cs
Normal file
21
Kasbot.APP/Extensions/IListExtensions.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Kasbot.Extensions
|
||||
{
|
||||
public static class IListExtensions
|
||||
{
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
|
||||
{
|
||||
var rng = new Random();
|
||||
|
||||
var buffer = source.ToList();
|
||||
for (int i = 0; i < buffer.Count; i++)
|
||||
{
|
||||
int j = rng.Next(i, buffer.Count);
|
||||
yield return buffer[j];
|
||||
|
||||
buffer[j] = buffer[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user