diff --git a/server/modules/GameManager.js b/server/modules/GameManager.js index 95fbe67..589113d 100644 --- a/server/modules/GameManager.js +++ b/server/modules/GameManager.js @@ -379,7 +379,7 @@ function initializePeopleForGame (uniqueCards, moderator) { } } - cards = shuffleArray(cards); // The deck should probably be shuffled, ey?. + cards = shuffle(cards); // The deck should probably be shuffled, ey?. let j = 0; if (moderator.userType === globals.USER_TYPES.TEMPORARY_MODERATOR) { // temporary moderators should be dealt in. @@ -410,13 +410,20 @@ function initializePeopleForGame (uniqueCards, moderator) { return people; } -function shuffleArray (array) { - for (let i = 0; i < array.length; i ++) { - const randIndex = Math.floor(Math.random() * i); - const temp = array[i]; - array[i] = array[randIndex]; - array[randIndex] = temp; +/* +-- To shuffle an array a of n elements (indices 0..n-1): +for i from nāˆ’1 downto 1 do + j ← random integer such that 0 ≤ j ≤ i + exchange a[j] and a[i] + */ +function shuffle(array) { + for (let i = array.length - 1; i > 0; i --) { + const j = Math.floor(Math.random() * (i + 1)); + const temp = array[j]; + array[j] = array[i]; + array[i] = temp; } + return array; }