refactor : removed all instances of new Random() and replaced with RandomUtil for a ThreadLocal randomizer

This commit is contained in:
ingmargoudt 2016-09-05 15:03:47 +02:00
parent f2cc8d4571
commit e2a479255a
51 changed files with 173 additions and 133 deletions

View file

@ -124,6 +124,7 @@ import mage.target.TargetPermanent;
import mage.target.TargetPlayer;
import mage.util.GameLog;
import mage.util.MessageToClient;
import mage.util.RandomUtil;
import mage.util.functions.ApplyToPermanent;
import mage.watchers.Watchers;
import mage.watchers.common.BlockedAttackerWatcher;
@ -159,7 +160,6 @@ public abstract class GameImpl implements Game, Serializable {
FILTER_LEGENDARY.add(new SupertypePredicate("Legendary"));
}
private static Random rnd = new Random();
private transient Object customData;
protected boolean simulation = false;
@ -1098,7 +1098,7 @@ public abstract class GameImpl implements Game, Serializable {
UUID[] players = getPlayers().keySet().toArray(new UUID[0]);
UUID playerId;
while (!hasEnded()) {
playerId = players[rnd.nextInt(players.length)];
playerId = players[RandomUtil.nextInt(players.length)];
Player player = getPlayer(playerId);
if (player != null && player.isInGame()) {
fireInformEvent(state.getPlayer(playerId).getLogName() + " won the toss");

View file

@ -34,6 +34,7 @@ import mage.cards.Card;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.util.RandomUtil;
import org.apache.log4j.Logger;
/**
@ -71,7 +72,6 @@ public abstract class DraftCube {
private static final Logger logger = Logger.getLogger(DraftCube.class);
private static final Random rnd = new Random();
private final String name;
private final int boosterSize = 15;
@ -100,7 +100,7 @@ public abstract class DraftCube {
boolean done = false;
int notValid = 0;
while (!done) {
int index = rnd.nextInt(leftCubeCards.size());
int index = RandomUtil.nextInt(leftCubeCards.size());
CardIdentity cardId = leftCubeCards.get(index);
leftCubeCards.remove(index);
if (!cardId.getName().isEmpty()) {

View file

@ -121,6 +121,7 @@ public abstract class DraftImpl implements Draft {
for(UUID playerId : players.keySet()) {
table.add(playerId);
}
table.setCurrent(currentId);
}
if (oldDraftPlayer.isPicking()) {

View file

@ -45,6 +45,7 @@ import mage.game.result.ResultProtos.MatchProto;
import mage.game.result.ResultProtos.MatchQuitStatus;
import mage.players.Player;
import mage.util.DateFormat;
import mage.util.RandomUtil;
import org.apache.log4j.Logger;
/**
@ -243,7 +244,7 @@ public abstract class MatchImpl implements Match {
}
protected void shufflePlayers() {
Collections.shuffle(this.players, new Random());
Collections.shuffle(this.players, RandomUtil.getRandom());
}
@Override

View file

@ -31,6 +31,7 @@ package mage.game.permanent.token;
import java.util.Random;
import mage.constants.CardType;
import mage.MageInt;
import mage.util.RandomUtil;
/**
*
@ -41,7 +42,7 @@ public class CentaurToken extends Token {
public CentaurToken() {
super("Centaur", "3/3 green Centaur creature token");
cardType.add(CardType.CREATURE);
setTokenType(new Random().nextInt(2) +1); // randomly take image 1 or 2
setTokenType(RandomUtil.nextInt(2) +1); // randomly take image 1 or 2
color.setGreen(true);
subtype.add("Centaur");
power = new MageInt(3);

View file

@ -38,6 +38,7 @@ import mage.abilities.costs.mana.GenericManaCost;
import mage.abilities.effects.common.DrawCardSourceControllerEffect;
import mage.constants.CardType;
import mage.constants.Zone;
import mage.util.RandomUtil;
/**
*
@ -69,10 +70,10 @@ public class ClueArtifactToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("SOI")) {
this.setTokenType(new Random().nextInt(6) + 1); // 6 different images
this.setTokenType(RandomUtil.nextInt(6) + 1); // 6 different images
}
if (getOriginalExpansionSetCode().equals("EDM")) {
this.setTokenType(new Random().nextInt(6) + 1); // 6 different images
this.setTokenType(RandomUtil.nextInt(6) + 1); // 6 different images
}
}

View file

@ -37,6 +37,7 @@ import mage.abilities.costs.common.SacrificeSourceCost;
import mage.abilities.mana.SimpleManaAbility;
import mage.constants.CardType;
import mage.constants.Zone;
import mage.util.RandomUtil;
/**
*
@ -65,10 +66,10 @@ public class EldraziScionToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("BFZ")) {
this.setTokenType(new Random().nextInt(3) + 1); // 3 different images
this.setTokenType(RandomUtil.nextInt(3) + 1); // 3 different images
}
if (getOriginalExpansionSetCode().equals("OGW")) {
this.setTokenType(new Random().nextInt(6) + 1); // 6 different images
this.setTokenType(RandomUtil.nextInt(6) + 1); // 6 different images
}
}

View file

@ -37,6 +37,7 @@ import mage.abilities.costs.common.SacrificeSourceCost;
import mage.abilities.mana.SimpleManaAbility;
import mage.constants.CardType;
import mage.constants.Zone;
import mage.util.RandomUtil;
/**
*
@ -61,6 +62,6 @@ public class EldraziSpawnToken extends Token {
availableImageSetCodes = tokenImageSets;
// Get one of the three possible token images
this.setTokenType(new Random().nextInt(3) + 1);
this.setTokenType(RandomUtil.nextInt(3) + 1);
}
}

View file

@ -33,6 +33,7 @@ import java.util.List;
import java.util.Random;
import mage.MageInt;
import mage.constants.CardType;
import mage.util.RandomUtil;
/**
*
@ -62,7 +63,7 @@ public class SoldierToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("THS")) {
this.setTokenType(new Random().nextInt(2) + 1);
this.setTokenType(RandomUtil.nextInt(2) + 1);
}
}

View file

@ -34,6 +34,7 @@ import java.util.Random;
import mage.MageInt;
import mage.abilities.keyword.FlyingAbility;
import mage.constants.CardType;
import mage.util.RandomUtil;
/**
*
@ -63,7 +64,7 @@ public class ThopterColorlessToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("ORI")) {
this.setTokenType(new Random().nextInt(2) + 1);
this.setTokenType(RandomUtil.nextInt(2) + 1);
}
}

View file

@ -46,6 +46,7 @@ import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentToken;
import mage.players.Player;
import mage.util.RandomUtil;
public class Token extends MageObjectImpl {
@ -290,7 +291,7 @@ public class Token extends MageObjectImpl {
// we should not set random set if appropriate set is already used
if (getOriginalExpansionSetCode() == null || getOriginalExpansionSetCode().isEmpty()
|| !availableImageSetCodes.contains(getOriginalExpansionSetCode())) {
setOriginalExpansionSetCode(availableImageSetCodes.get(new Random().nextInt(availableImageSetCodes.size())));
setOriginalExpansionSetCode(availableImageSetCodes.get(RandomUtil.nextInt(availableImageSetCodes.size())));
}
}
} else {

View file

@ -32,6 +32,7 @@ import java.util.Arrays;
import java.util.Random;
import mage.MageInt;
import mage.constants.CardType;
import mage.util.RandomUtil;
/**
*
@ -62,7 +63,7 @@ public class WarriorToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("KTK")) {
this.setTokenType(new Random().nextInt(2) + 1);
this.setTokenType(RandomUtil.nextInt(2) + 1);
}
}
}

View file

@ -33,6 +33,7 @@ import java.util.List;
import java.util.Random;
import mage.MageInt;
import mage.constants.CardType;
import mage.util.RandomUtil;
/**
*
@ -61,13 +62,13 @@ public class ZombieToken extends Token {
public void setExpansionSetCodeForImage(String code) {
super.setExpansionSetCodeForImage(code);
if (getOriginalExpansionSetCode().equals("ISD")) {
this.setTokenType(new Random().nextInt(3) + 1);
this.setTokenType(RandomUtil.nextInt(3) + 1);
}
if (getOriginalExpansionSetCode().equals("C14")) {
this.setTokenType(2);
}
if (getOriginalExpansionSetCode().equals("EMN")) {
this.setTokenType(new Random().nextInt(4) + 1);
this.setTokenType(RandomUtil.nextInt(4) + 1);
}
}

View file

@ -57,6 +57,7 @@ import mage.game.result.ResultProtos.MatchQuitStatus;
import mage.game.result.ResultProtos.TourneyProto;
import mage.game.result.ResultProtos.TourneyRoundProto;
import mage.players.Player;
import mage.util.RandomUtil;
import org.apache.log4j.Logger;
/**
@ -68,7 +69,6 @@ public abstract class TournamentImpl implements Tournament {
protected UUID id = UUID.randomUUID();
protected List<Round> rounds = new CopyOnWriteArrayList<>();
protected Map<UUID, TournamentPlayer> players = new HashMap<>();
protected static Random rnd = new Random();
protected String matchName;
protected TournamentOptions options;
protected TournamentType tournamentType;
@ -217,7 +217,7 @@ public abstract class TournamentImpl implements Tournament {
private TournamentPlayer getNextAvailablePlayer(List<TournamentPlayer> roundPlayers, List<TournamentPlayer> playerWithByes) {
TournamentPlayer nextPlayer;
if (playerWithByes.isEmpty()) {
int i = rnd.nextInt(roundPlayers.size());
int i = RandomUtil.nextInt(roundPlayers.size());
nextPlayer = roundPlayers.get(i);
roundPlayers.remove(i);
} else { // prefer players with byes to pair