From 645d70e6cea022fcae3083e244f51e3cc54cc7d5 Mon Sep 17 00:00:00 2001 From: Meerkov Date: Mon, 16 Apr 2018 20:40:20 -0700 Subject: [PATCH] Fixes inefficient shuffle algorithm. Note that shuffling 13 times in a row is not necessary. Collections.shuffle will produce every possible shuffle with equal probability already. --- .../main/java/forge/game/player/Player.java | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/forge-game/src/main/java/forge/game/player/Player.java b/forge-game/src/main/java/forge/game/player/Player.java index df9a75eab5e..4813494144d 100644 --- a/forge-game/src/main/java/forge/game/player/Player.java +++ b/forge-game/src/main/java/forge/game/player/Player.java @@ -1608,26 +1608,8 @@ public class Player extends GameEntity implements Comparable { return; } - // overdone but wanted to make sure it was really random - final Random random = MyRandom.getRandom(); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - - int s = list.size(); - for (int i = 0; i < s; i++) { - list.add(random.nextInt(s - 1), list.remove(random.nextInt(s))); - } - - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); - Collections.shuffle(list, random); + // Note: Shuffling once is sufficient. + Collections.shuffle(list, MyRandom.getRandom()); getZone(ZoneType.Library).setCards(getController().cheatShuffle(list));