From e17a32030134d4f5da68411e7c33098586aa2d10 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 1 May 2016 18:13:12 +0200 Subject: [PATCH] Shuffling stuff larger than 256 will work properly now. Thanks to Joe4evr for finding the bug --- NadekoBot/Classes/Extensions.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index fea6b61a..c3062c5f 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -138,14 +138,21 @@ namespace NadekoBot.Extensions /// public static void Shuffle(this IList list) { + + // Thanks to @Joe4Evr for finding a bug in the old version of the shuffle var provider = new RNGCryptoServiceProvider(); var n = list.Count; while (n > 1) { - var box = new byte[1]; - do provider.GetBytes(box); - while (!(box[0] < n * (byte.MaxValue / n))); - var k = (box[0] % n); + var box = new byte[(n / Byte.MaxValue) + 1]; + int boxSum; + do + { + provider.GetBytes(box); + boxSum = box.Sum(b => b); + } + while (!(boxSum < n * ((Byte.MaxValue * box.Length) / n))); + var k = (boxSum % n); n--; var value = list[k]; list[k] = list[n];