From 2c4bac0c89168e0e517241402e4cfdb41f7d2ef5 Mon Sep 17 00:00:00 2001 From: charlescharles Date: Tue, 28 Oct 2014 16:17:58 -0400 Subject: [PATCH] Fix Knuth shuffle --- lib/torrent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/torrent.js b/lib/torrent.js index c55cebc5..7659314e 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -986,8 +986,8 @@ function randomInt (high) { function randomizedForEach (array, cb) { var indices = array.map(function (value, index) { return index }) - for (var i = 0, len = indices.length; i < len; ++i) { - var j = randomInt(len) + for (var i = indices.length - 1; i > 0; --i) { + var j = randomInt(i + 1) var tmp = indices[i] indices[i] = indices[j] indices[j] = tmp