From 5c9a27c930ab9fd6e5c015058a124a496093b229 Mon Sep 17 00:00:00 2001 From: Meerkov Date: Mon, 16 Apr 2018 21:26:12 -0700 Subject: [PATCH] Update lots of code to use MyRandom directly. Some parts of the code were using the normal random, instead of going through the MyRandom class. This makes it much harder to change the RNG method later. For example, you may want to change the RNG to always be seeded with the same number, for running AI experiments. This change makes that easier. --- .../deck/generation/DeckGenerator2Color.java | 8 ++++-- .../deck/generation/DeckGenerator3Color.java | 4 +-- .../deck/generation/DeckGenerator4Color.java | 4 +-- .../deck/generation/DeckGeneratorBase.java | 5 ++-- .../generation/DeckGeneratorMonoColor.java | 4 ++- .../item/generation/BoosterGenerator.java | 11 ++++---- .../src/main/java/forge/util/Aggregates.java | 11 +++----- .../main/java/forge/util/NameGenerator.java | 28 +++++++++---------- forge-game/src/main/java/forge/game/Game.java | 5 ++-- .../src/main/java/forge/game/Match.java | 8 ++---- .../ability/effects/ChooseNumberEffect.java | 5 ++-- .../game/ability/effects/DigUntilEffect.java | 3 +- .../ability/effects/ReorderZoneEffect.java | 4 +-- .../java/forge/deck/DeckGeneratorTheme.java | 6 ++-- .../src/main/java/forge/deck/DeckgenUtil.java | 23 +++++++-------- .../forge/limited/CardThemedDeckBuilder.java | 22 +++++++-------- .../java/forge/limited/SealedDeckBuilder.java | 8 ++---- .../main/java/forge/model/UnOpenedMeta.java | 4 +-- .../forge/quest/QuestEventDuelManager.java | 5 ++-- .../java/forge/quest/io/ReadPriceList.java | 10 +++---- .../main/java/forge/sound/MusicPlaylist.java | 5 ++-- 21 files changed, 81 insertions(+), 102 deletions(-) diff --git a/forge-core/src/main/java/forge/deck/generation/DeckGenerator2Color.java b/forge-core/src/main/java/forge/deck/generation/DeckGenerator2Color.java index 525f0bc3161..86fade5a990 100644 --- a/forge-core/src/main/java/forge/deck/generation/DeckGenerator2Color.java +++ b/forge-core/src/main/java/forge/deck/generation/DeckGenerator2Color.java @@ -26,6 +26,8 @@ import forge.deck.CardPool; import forge.deck.DeckFormat; import forge.item.PaperCard; +import forge.util.MyRandom; + import org.apache.commons.lang3.tuple.ImmutablePair; import java.util.Arrays; @@ -85,13 +87,13 @@ public class DeckGenerator2Color extends DeckGeneratorBase { format0.adjustCMCLevels(cmcLevels); if( c1 == 0 && c2 == 0) { - int color1 = r.nextInt(5); - int color2 = (color1 + 1 + r.nextInt(4)) % 5; + int color1 = MyRandom.getRandom().nextInt(5); + int color2 = (color1 + 1 + MyRandom.getRandom().nextInt(4)) % 5; colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2); } else if ( c1 == 0 || c2 == 0 ) { byte knownColor = (byte) (c1 | c2); int color1 = Arrays.binarySearch(MagicColor.WUBRG, knownColor); - int color2 = (color1 + 1 + r.nextInt(4)) % 5; + int color2 = (color1 + 1 + MyRandom.getRandom().nextInt(4)) % 5; colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2); } else { colors = ColorSet.fromMask(c1 | c2); diff --git a/forge-core/src/main/java/forge/deck/generation/DeckGenerator3Color.java b/forge-core/src/main/java/forge/deck/generation/DeckGenerator3Color.java index 5b779f278ef..1dbaf4e3d35 100644 --- a/forge-core/src/main/java/forge/deck/generation/DeckGenerator3Color.java +++ b/forge-core/src/main/java/forge/deck/generation/DeckGenerator3Color.java @@ -87,8 +87,8 @@ public class DeckGenerator3Color extends DeckGeneratorBase { return; case 0: - int color1 = r.nextInt(5); - int color2 = (color1 + 1 + r.nextInt(4)) % 5; + int color1 = MyRandom.getRandom().nextInt(5); + int color2 = (color1 + 1 + MyRandom.getRandom().nextInt(4)) % 5; colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2).inverse(); return; diff --git a/forge-core/src/main/java/forge/deck/generation/DeckGenerator4Color.java b/forge-core/src/main/java/forge/deck/generation/DeckGenerator4Color.java index 1a0e0b36c94..b0fb26bb4b4 100644 --- a/forge-core/src/main/java/forge/deck/generation/DeckGenerator4Color.java +++ b/forge-core/src/main/java/forge/deck/generation/DeckGenerator4Color.java @@ -86,8 +86,8 @@ public class DeckGenerator4Color extends DeckGeneratorBase { return; case 0: - int color1 = r.nextInt(5); - int color2 = (color1 + 1 + r.nextInt(4)) % 5; + int color1 = MyRandom.getRandom().nextInt(5); + int color2 = (color1 + 1 + MyRandom.getRandom().nextInt(4)) % 5; colors = ColorSet.fromMask(MagicColor.WHITE << color1 | MagicColor.WHITE << color2).inverse(); return; diff --git a/forge-core/src/main/java/forge/deck/generation/DeckGeneratorBase.java b/forge-core/src/main/java/forge/deck/generation/DeckGeneratorBase.java index 62f8923e53f..57731ac55db 100644 --- a/forge-core/src/main/java/forge/deck/generation/DeckGeneratorBase.java +++ b/forge-core/src/main/java/forge/deck/generation/DeckGeneratorBase.java @@ -51,7 +51,6 @@ import java.util.regex.Pattern; */ public abstract class DeckGeneratorBase { protected final DebugTrace trace = new DebugTrace(); - protected final Random r = MyRandom.getRandom(); protected final Map cardCounts = new HashMap(); protected int maxDuplicates = 4; protected boolean useArtifacts = true; @@ -133,7 +132,7 @@ public abstract class DeckGeneratorBase { int res = 0; while (res < cnt) { - PaperCard cp = source.get(r.nextInt(srcLen)); + PaperCard cp = source.get(MyRandom.getRandom().nextInt(srcLen)); int newCount = cardCounts.get(cp.getName()) + 1; //add card to deck if not already maxed out on card @@ -166,7 +165,7 @@ public abstract class DeckGeneratorBase { int res = 0; while (res < cnt) { - String s = source.get(r.nextInt(srcLen)); + String s = source.get(MyRandom.getRandom().nextInt(srcLen)); int newCount = cardCounts.get(s) + 1; //add card to deck if not already maxed out on card diff --git a/forge-core/src/main/java/forge/deck/generation/DeckGeneratorMonoColor.java b/forge-core/src/main/java/forge/deck/generation/DeckGeneratorMonoColor.java index 6c0c414096d..b577c0de2ac 100644 --- a/forge-core/src/main/java/forge/deck/generation/DeckGeneratorMonoColor.java +++ b/forge-core/src/main/java/forge/deck/generation/DeckGeneratorMonoColor.java @@ -26,6 +26,8 @@ import forge.deck.CardPool; import forge.deck.DeckFormat; import forge.item.PaperCard; +import forge.util.MyRandom; + import org.apache.commons.lang3.tuple.ImmutablePair; import java.util.List; @@ -79,7 +81,7 @@ public class DeckGeneratorMonoColor extends DeckGeneratorBase { public void initialize(final String clr1){ if (MagicColor.fromName(clr1) == 0) { - int color1 = r.nextInt(5); + int color1 = MyRandom.getRandom().nextInt(5); colors = ColorSet.fromMask(MagicColor.WHITE << color1); } else { colors = ColorSet.fromNames(clr1); diff --git a/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java b/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java index 3cc2ef96ee8..67f092e90e4 100644 --- a/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java +++ b/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java @@ -83,7 +83,6 @@ public class BoosterGenerator { && edition.getFoilType() != FoilType.NOT_SUPPORTED; boolean foilAtEndOfPack = hasFoil && edition.getFoilAlwaysInCommonSlot(); - Random rand = new Random(); // Foil chances // 1 Rare or Mythic rare (distribution ratio same as nonfoils) // 2-3 Uncommons @@ -96,7 +95,7 @@ public class BoosterGenerator { // Other special types of foil slots, add here CardRarity foilCard = CardRarity.Unknown; while (foilCard == CardRarity.Unknown) { - int randomNum = rand.nextInt(10) + 1; + int randomNum = MyRandom.getRandom().nextInt(10) + 1; switch (randomNum) { case 1: // Rare or Mythic @@ -126,7 +125,7 @@ public class BoosterGenerator { if (edition.getName().equals("Vintage Masters")) { // 1 in 53 packs, with 7 possibilities for the slot itself in VMA // (1 RareMythic, 2 Uncommon, 3 Common, 1 Special) - if (rand.nextInt(53) <= 7) { + if (MyRandom.getRandom().nextInt(53) <= 7) { foilCard = CardRarity.Special; } } @@ -147,7 +146,7 @@ public class BoosterGenerator { // so 3 out of the 53 rares in the set. // while information cannot be found, my personal (subjective) experience from that time was // that they were indeed similar chance, at least not significantly less. - if (rand.nextInt(53) <= 3) { + if (MyRandom.getRandom().nextInt(53) <= 3) { foilCard = CardRarity.Special; } } @@ -200,7 +199,7 @@ public class BoosterGenerator { // According to information I found, Basic Lands // are on the common foil sheet, each type appearing once. // Large Sets usually have 110 commons and 20 lands. - if (rand.nextInt(130) <= 20) { + if (MyRandom.getRandom().nextInt(130) <= 20) { foilSlot = BoosterSlots.BASIC_LAND; } } @@ -307,7 +306,7 @@ public class BoosterGenerator { // 1 out of ~30 normal and mythic rares are foil, // match that. // If not special card, make it always foil. - if ((rand.nextInt(30) == 1) || (foilSlot != BoosterSlots.SPECIAL)) { + if ((MyRandom.getRandom().nextInt(30) == 1) || (foilSlot != BoosterSlots.SPECIAL)) { foilCardGeneratedAndHeld.add(generateFoilCard(ps)); } else { // Otherwise it's not foil (even though this is the diff --git a/forge-core/src/main/java/forge/util/Aggregates.java b/forge-core/src/main/java/forge/util/Aggregates.java index a86a738f9cf..fb13b81400c 100644 --- a/forge-core/src/main/java/forge/util/Aggregates.java +++ b/forge-core/src/main/java/forge/util/Aggregates.java @@ -101,21 +101,20 @@ public class Aggregates { public static final T random(final Iterable source) { if (source == null) { return null; } - Random rnd = MyRandom.getRandom(); if (source instanceof List) { List src = (List)source; int len = src.size(); switch(len) { case 0: return null; case 1: return src.get(0); - default: return src.get(rnd.nextInt(len)); + default: return src.get(MyRandom.getRandom().nextInt(len)); } } T candidate = null; int lowest = Integer.MAX_VALUE; for (final T item : source) { - int next = rnd.nextInt(); + int next = MyRandom.getRandom().nextInt(); if(next < lowest) { lowest = next; candidate = item; @@ -129,7 +128,6 @@ public class Aggregates { } public static final > L random(final Iterable source, final int count, final L list) { // Using Reservoir Sampling to grab X random values from source - Random rnd = MyRandom.getRandom(); int i = 0; for (T item : source) { i++; @@ -138,7 +136,7 @@ public class Aggregates { list.add(item); } else { // Progressively reduce odds of item > count to get added into the reservoir - int j = rnd.nextInt(i); + int j = MyRandom.getRandom().nextInt(i); if (j < count) { list.set(j, item); } @@ -161,8 +159,7 @@ public class Aggregates { } public static int randomInt(int min, int max) { - Random rnd = MyRandom.getRandom(); - return rnd.nextInt(max - min + 1) + min; + return MyRandom.getRandom().nextInt(max - min + 1) + min; } public static final Iterable uniqueByLast(final Iterable source, final Function fnUniqueKey) { // this might be exotic diff --git a/forge-core/src/main/java/forge/util/NameGenerator.java b/forge-core/src/main/java/forge/util/NameGenerator.java index 1813170efef..6e7512ed844 100644 --- a/forge-core/src/main/java/forge/util/NameGenerator.java +++ b/forge-core/src/main/java/forge/util/NameGenerator.java @@ -6,7 +6,6 @@ import org.apache.commons.lang3.ArrayUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Random; /** * @author Marc @@ -202,8 +201,8 @@ public final class NameGenerator { /** Generates a single name that doesn't match the specified name. * - * @param gender String specifying the desired gender. Recognises "Male", "Female" and "Any". - * @param type String specifying the desired type. Recognises "Fantasy", "Generic" and "Any". + * @param gender String specifying the desired gender. Recognizes "Male", "Female" and "Any". + * @param type String specifying the desired type. Recognizes "Fantasy", "Generic" and "Any". * @param excludeNames A list of names already being used. * @return Returns the generated name string. */ @@ -211,33 +210,32 @@ public final class NameGenerator { usedNames = excludeNames; String name = ""; - Random seed = MyRandom.getRandom(); boolean useMoniker = false; switch (type + gender) { - case "GenericMale": { sourceList = genericMales; useMoniker = seed.nextFloat() <= 0.03f; break;} - case "GenericFemale": { sourceList = genericFemales; useMoniker = seed.nextFloat() <= 0.02f; break;} - case "FantasyMale": { sourceList = fantasyMales; useMoniker = seed.nextFloat() <= 0.10f; break;} - case "FantasyFemale": { sourceList = fantasyFemales; useMoniker = seed.nextFloat() <= 0.08f; break;} + case "GenericMale": { sourceList = genericMales; useMoniker = MyRandom.getRandom().nextFloat() <= 0.03f; break;} + case "GenericFemale": { sourceList = genericFemales; useMoniker = MyRandom.getRandom().nextFloat() <= 0.02f; break;} + case "FantasyMale": { sourceList = fantasyMales; useMoniker = MyRandom.getRandom().nextFloat() <= 0.10f; break;} + case "FantasyFemale": { sourceList = fantasyFemales; useMoniker = MyRandom.getRandom().nextFloat() <= 0.08f; break;} case "AnyMale": sourceList = getCombinedLists(genericMales, fantasyMales); - useMoniker = seed.nextFloat() <= 0.06f; + useMoniker = MyRandom.getRandom().nextFloat() <= 0.06f; break; case "AnyFemale": sourceList = getCombinedLists(genericFemales, fantasyFemales); - useMoniker = seed.nextFloat() <= 0.025f; + useMoniker = MyRandom.getRandom().nextFloat() <= 0.025f; break; case "GenericAny": sourceList = getCombinedLists(genericMales, genericFemales); - useMoniker = seed.nextFloat() <= 0.015f; + useMoniker = MyRandom.getRandom().nextFloat() <= 0.015f; break; case "FantasyAny": sourceList = getCombinedLists(fantasyMales, fantasyFemales); - useMoniker = seed.nextFloat() <= 0.06f; + useMoniker = MyRandom.getRandom().nextFloat() <= 0.06f; break; default: @@ -248,12 +246,12 @@ public final class NameGenerator { Collections.addAll(all, genericFemales); Collections.addAll(all, fantasyFemales); sourceList = all.toArray(new String[all.size()]); - useMoniker = seed.nextFloat() <= 0.04f; + useMoniker = MyRandom.getRandom().nextFloat() <= 0.04f; break; } do { - name = sourceList[seed.nextInt(sourceList.length)]; + name = sourceList[MyRandom.getRandom().nextInt(sourceList.length)]; } while (excludeNames.contains(name)); usedNames.add(name); // add base name to used names list @@ -261,7 +259,7 @@ public final class NameGenerator { if (useMoniker) { String moniker = ""; do { - moniker = monikers[seed.nextInt(monikers.length)]; + moniker = monikers[MyRandom.getRandom().nextInt(monikers.length)]; } while (usedMonikers.contains(moniker)); usedMonikers.add(moniker); diff --git a/forge-game/src/main/java/forge/game/Game.java b/forge-game/src/main/java/forge/game/Game.java index f1e3fcf12d0..e0db5a969e0 100644 --- a/forge-game/src/main/java/forge/game/Game.java +++ b/forge-game/src/main/java/forge/game/Game.java @@ -50,6 +50,7 @@ import forge.game.zone.Zone; import forge.game.zone.ZoneType; import forge.trackable.Tracker; import forge.util.Aggregates; +import forge.util.MyRandom; import forge.util.Visitor; import java.util.*; @@ -797,7 +798,7 @@ public class Game { onePlayerHasTimeShifted = false; } - CardRarity anteRarity = validRarities.get(new Random().nextInt(validRarities.size())); + CardRarity anteRarity = validRarities.get(MyRandom.getRandom().nextInt(validRarities.size())); System.out.println("Rarity chosen for ante: " + anteRarity.name()); @@ -827,7 +828,7 @@ public class Game { library.removeAll((Collection)toRemove); if (library.size() > 0) { //Make sure that matches were found. If not, use the original method to choose antes - Card ante = library.get(new Random().nextInt(library.size())); + Card ante = library.get(MyRandom.getRandom().nextInt(library.size())); anteed.put(player, ante); } else { chooseRandomCardsForAnte(player, anteed); diff --git a/forge-game/src/main/java/forge/game/Match.java b/forge-game/src/main/java/forge/game/Match.java index e17e22234a2..46dae84d013 100644 --- a/forge-game/src/main/java/forge/game/Match.java +++ b/forge-game/src/main/java/forge/game/Match.java @@ -179,7 +179,7 @@ public class Match { return myRemovedAnteCards; } - private static void preparePlayerLibrary(Player player, final ZoneType zoneType, CardPool section, boolean canRandomFoil, Random generator) { + private static void preparePlayerLibrary(Player player, final ZoneType zoneType, CardPool section, boolean canRandomFoil) { PlayerZone library = player.getZone(zoneType); List newLibrary = new ArrayList(); for (final Entry stackOfCards : section) { @@ -245,11 +245,9 @@ public class Match { } } - Random generator = MyRandom.getRandom(); - - preparePlayerLibrary(player, ZoneType.Library, myDeck.getMain(), psc.useRandomFoil(), generator); + preparePlayerLibrary(player, ZoneType.Library, myDeck.getMain(), psc.useRandomFoil()); if (myDeck.has(DeckSection.Sideboard)) { - preparePlayerLibrary(player, ZoneType.Sideboard, myDeck.get(DeckSection.Sideboard), psc.useRandomFoil(), generator); + preparePlayerLibrary(player, ZoneType.Sideboard, myDeck.get(DeckSection.Sideboard), psc.useRandomFoil()); } player.initVariantsZones(psc); diff --git a/forge-game/src/main/java/forge/game/ability/effects/ChooseNumberEffect.java b/forge-game/src/main/java/forge/game/ability/effects/ChooseNumberEffect.java index 86e03347609..ce31edd85ad 100644 --- a/forge-game/src/main/java/forge/game/ability/effects/ChooseNumberEffect.java +++ b/forge-game/src/main/java/forge/game/ability/effects/ChooseNumberEffect.java @@ -9,11 +9,11 @@ import forge.game.player.Player; import forge.game.spellability.AbilitySub; import forge.game.spellability.SpellAbility; import forge.game.spellability.TargetRestrictions; +import forge.util.MyRandom; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Random; public class ChooseNumberEffect extends SpellAbilityEffect { @@ -54,8 +54,7 @@ public class ChooseNumberEffect extends SpellAbilityEffect { if ((tgt == null) || p.canBeTargetedBy(sa)) { int chosen; if (random) { - final Random randomGen = new Random(); - chosen = randomGen.nextInt(max - min) + min; + chosen = MyRandom.getRandom().nextInt(max - min) + min; p.getGame().getAction().nofityOfValue(sa, p, Integer.toString(chosen), null); } else { String title = sa.hasParam("ListTitle") ? sa.getParam("ListTitle") : "Choose a number"; diff --git a/forge-game/src/main/java/forge/game/ability/effects/DigUntilEffect.java b/forge-game/src/main/java/forge/game/ability/effects/DigUntilEffect.java index 8fd20f2f07d..73a32885eed 100644 --- a/forge-game/src/main/java/forge/game/ability/effects/DigUntilEffect.java +++ b/forge-game/src/main/java/forge/game/ability/effects/DigUntilEffect.java @@ -179,8 +179,7 @@ public class DigUntilEffect extends SpellAbilityEffect { } } if (sa.hasParam("RevealRandomOrder")) { - final Random random = MyRandom.getRandom(); - Collections.shuffle(revealed, random); + Collections.shuffle(revealed, MyRandom.getRandom()); } if (sa.hasParam("NoneFoundDestination") && found.size() < untilAmount) { diff --git a/forge-game/src/main/java/forge/game/ability/effects/ReorderZoneEffect.java b/forge-game/src/main/java/forge/game/ability/effects/ReorderZoneEffect.java index b2f46ed3ae0..e236e8d391a 100644 --- a/forge-game/src/main/java/forge/game/ability/effects/ReorderZoneEffect.java +++ b/forge-game/src/main/java/forge/game/ability/effects/ReorderZoneEffect.java @@ -11,7 +11,6 @@ import forge.util.MyRandom; import java.util.Collections; import java.util.List; -import java.util.Random; public class ReorderZoneEffect extends SpellAbilityEffect { @Override @@ -33,8 +32,7 @@ public class ReorderZoneEffect extends SpellAbilityEffect { if ((tgt == null) || p.canBeTargetedBy(sa)) { CardCollection list = new CardCollection(p.getCardsIn(zone)); if (shuffle) { - final Random ran = MyRandom.getRandom(); - Collections.shuffle(list, ran); + Collections.shuffle(list, MyRandom.getRandom()); p.getZone(zone).setCards(list); } else { diff --git a/forge-gui/src/main/java/forge/deck/DeckGeneratorTheme.java b/forge-gui/src/main/java/forge/deck/DeckGeneratorTheme.java index b9515d7f081..7b49a832d34 100644 --- a/forge-gui/src/main/java/forge/deck/DeckGeneratorTheme.java +++ b/forge-gui/src/main/java/forge/deck/DeckGeneratorTheme.java @@ -27,7 +27,6 @@ import forge.util.MyRandom; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.Random; /** *

@@ -120,7 +119,6 @@ public class DeckGeneratorTheme extends DeckGeneratorBase { final List groups = readGroups(lines); // begin assigning cards to the deck - final Random r = MyRandom.getRandom(); for (int i = 0; i < groups.size(); i++) { final Grp g = groups.get(i); @@ -130,14 +128,14 @@ public class DeckGeneratorTheme extends DeckGeneratorBase { errorBuilder.append("Group" + i + ":" + grpCnt + "\n"); for (int j = 0; j < grpCnt; j++) { - s = g.cardnames.get(r.nextInt(cnSize)); + s = g.cardnames.get(MyRandom.getRandom().nextInt(cnSize)); ss = s.split("\\|"); int lc = 0; while ((cardCounts.get(ss[0]) >= g.maxCnt) || (lc > 999)) { // looping // forever - s = g.cardnames.get(r.nextInt(cnSize)); + s = g.cardnames.get(MyRandom.getRandom().nextInt(cnSize)); ss = s.split("\\|"); lc++; } diff --git a/forge-gui/src/main/java/forge/deck/DeckgenUtil.java b/forge-gui/src/main/java/forge/deck/DeckgenUtil.java index 8e1d2684666..a0f5e96816d 100644 --- a/forge-gui/src/main/java/forge/deck/DeckgenUtil.java +++ b/forge-gui/src/main/java/forge/deck/DeckgenUtil.java @@ -43,10 +43,9 @@ import java.util.*; public class DeckgenUtil { public static Deck buildCardGenDeck(GameFormat format, boolean isForAI){ - Random random = new Random(); try { List keys = new ArrayList<>(CardRelationMatrixGenerator.cardPools.get(format.getName()).keySet()); - String randomKey = keys.get( random.nextInt(keys.size()) ); + String randomKey = keys.get( MyRandom.getRandom().nextInt(keys.size()) ); Predicate cardFilter = Predicates.and(format.getFilterPrinted(),PaperCard.Predicates.name(randomKey)); PaperCard keyCard = FModel.getMagicDb().getCommonCards().getAllCards(cardFilter).get(0); @@ -124,7 +123,6 @@ public class DeckgenUtil { Collections.sort(potentialCards,new CardDistanceComparator()); Collections.reverse(potentialCards); //get second keycard - Random r = new Random(); List preSelectedCards = new ArrayList<>(); for(Map.Entry pair:potentialCards){ preSelectedCards.add(pair.getKey()); @@ -146,7 +144,7 @@ public class DeckgenUtil { if(preSelectedCards.size()> potentialSecondCards = CardRelationMatrixGenerator.cardPools.get(format.getName()).get(secondKeycard.getName()); //combine card distances from second key card and re-sort @@ -166,7 +164,7 @@ public class DeckgenUtil { int removeCount=0; int i=0; for(PaperCard c:selectedCards){ - if(r.nextInt(100)>70+(15-(i/selectedCards.size())*selectedCards.size()) && removeCount<4 //randomly remove some cards - more likely as distance increases + if(MyRandom.getRandom().nextInt(100)>70+(15-(i/selectedCards.size())*selectedCards.size()) && removeCount<4 //randomly remove some cards - more likely as distance increases &&!c.getName().contains("Urza")){ //avoid breaking Tron decks toRemove.add(c); removeCount++; @@ -184,9 +182,9 @@ public class DeckgenUtil { List playsetList = new ArrayList<>(); int keyCardCount=4; if(card.getRules().getMainPart().getManaCost().getCMC()>7){ - keyCardCount=1+r.nextInt(4); + keyCardCount=1+MyRandom.getRandom().nextInt(4); }else if(card.getRules().getMainPart().getManaCost().getCMC()>5){ - keyCardCount=2+r.nextInt(3); + keyCardCount=2+MyRandom.getRandom().nextInt(3); } for(int j=0;j7){ - keyCard2Count=1+r.nextInt(4); + keyCard2Count=1+MyRandom.getRandom().nextInt(4); }else if(card.getRules().getMainPart().getManaCost().getCMC()>5){ - keyCard2Count=2+r.nextInt(3); + keyCard2Count=2+MyRandom.getRandom().nextInt(3); } for(int j=0;j preSelectedCards = new ArrayList<>(); for(Map.Entry pair:potentialCards){ @@ -557,7 +554,7 @@ public class DeckgenUtil { if(preSelectedCards.size()<75){ break; } - if(r.nextInt(100)>60+(15-(i/preSelectedCards.size())*preSelectedCards.size()) && removeCount<4 //randomly remove some cards - more likely as distance increases + if(MyRandom.getRandom().nextInt(100)>60+(15-(i/preSelectedCards.size())*preSelectedCards.size()) && removeCount<4 //randomly remove some cards - more likely as distance increases &&!c.getName().contains("Urza")&&!c.getName().contains("Wastes")){ //avoid breaking Tron decks toRemove.add(c); removeCount++; @@ -578,7 +575,7 @@ public class DeckgenUtil { colorList=colorListFiltered; } List cardList = Lists.newArrayList(colorList); - Collections.shuffle(cardList, new Random()); + Collections.shuffle(cardList, MyRandom.getRandom()); int shortlistlength=400; if(cardList.size() randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool)); - Collections.shuffle(randomPool,new Random()); + Collections.shuffle(randomPool, MyRandom.getRandom()); Iterator iRandomPool=randomPool.iterator(); while (deckList.size() < targetSize) { if (logToConsole) { @@ -685,9 +685,9 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase { if(colors.isColorless()){ minBasics=0; }else if(colors.isMonoColor()){ - minBasics=Math.round((r.nextInt(15)+9)*((float) targetSize) / 60); + minBasics=Math.round((MyRandom.getRandom().nextInt(15)+9)*((float) targetSize) / 60); }else{ - minBasics=Math.round((r.nextInt(8)+6)*((float) targetSize) / 60); + minBasics=Math.round((MyRandom.getRandom().nextInt(8)+6)*((float) targetSize) / 60); } @@ -706,7 +706,7 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase { } continue; } - if (!inverseDLands.contains(card.getName())&&!dLands.contains(card.getName())&&r.nextInt(100)<90) { + if (!inverseDLands.contains(card.getName())&&!dLands.contains(card.getName())&&MyRandom.getRandom().nextInt(100)<90) { landsToAdd.add(card); landsNeeded--; if (logToConsole) { @@ -741,7 +741,7 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase { } }; List randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool)); - Collections.shuffle(randomPool,new Random()); + Collections.shuffle(randomPool, MyRandom.getRandom()); Iterator iRandomPool=randomPool.iterator(); for(int i=0;i targetCMCs = new HashMap<>(); - targetCMCs.put(1,Math.round((r.nextInt(4)+2)*targetSize/60));//2 - targetCMCs.put(2,Math.round((r.nextInt(5)+5)*targetSize/60));//6 - targetCMCs.put(3,Math.round((r.nextInt(5)+6)*targetSize/60));//7 - targetCMCs.put(4,Math.round((r.nextInt(3)+3)*targetSize/60));//4 - targetCMCs.put(5,Math.round((r.nextInt(3)+3)*targetSize/60));//3 - targetCMCs.put(6,Math.round((r.nextInt(3)+1)*targetSize/60));//2 + targetCMCs.put(1,Math.round((MyRandom.getRandom().nextInt(4)+2)*targetSize/60));//2 + targetCMCs.put(2,Math.round((MyRandom.getRandom().nextInt(5)+5)*targetSize/60));//6 + targetCMCs.put(3,Math.round((MyRandom.getRandom().nextInt(5)+6)*targetSize/60));//7 + targetCMCs.put(4,Math.round((MyRandom.getRandom().nextInt(3)+3)*targetSize/60));//4 + targetCMCs.put(5,Math.round((MyRandom.getRandom().nextInt(3)+3)*targetSize/60));//3 + targetCMCs.put(6,Math.round((MyRandom.getRandom().nextInt(3)+1)*targetSize/60));//2 final Map creatureCosts = new HashMap(); diff --git a/forge-gui/src/main/java/forge/limited/SealedDeckBuilder.java b/forge-gui/src/main/java/forge/limited/SealedDeckBuilder.java index 0d03393b379..4a87f4ed6ab 100644 --- a/forge-gui/src/main/java/forge/limited/SealedDeckBuilder.java +++ b/forge-gui/src/main/java/forge/limited/SealedDeckBuilder.java @@ -11,7 +11,6 @@ import forge.util.MyRandom; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Random; /** * Deck builder for Sealed Deck Format. @@ -67,17 +66,16 @@ public class SealedDeckBuilder extends LimitedDeckBuilder { String color1; String color2; - final Random r = MyRandom.getRandom(); if (maxColors.size() > 1) { - int n = r.nextInt(maxColors.size()); + int n = MyRandom.getRandom().nextInt(maxColors.size()); color1 = maxColors.get(n); maxColors.remove(n); - n = r.nextInt(maxColors.size()); + n = MyRandom.getRandom().nextInt(maxColors.size()); color2 = maxColors.get(n); } else { color1 = maxColors.get(0); if (secondColors.size() > 1) { - color2 = secondColors.get(r.nextInt(secondColors.size())); + color2 = secondColors.get(MyRandom.getRandom().nextInt(secondColors.size())); } else { color2 = secondColors.get(0); } diff --git a/forge-gui/src/main/java/forge/model/UnOpenedMeta.java b/forge-gui/src/main/java/forge/model/UnOpenedMeta.java index 34d9f54e1ea..0a819c54d26 100644 --- a/forge-gui/src/main/java/forge/model/UnOpenedMeta.java +++ b/forge-gui/src/main/java/forge/model/UnOpenedMeta.java @@ -26,7 +26,6 @@ import forge.util.gui.SGuiChoose; import java.util.ArrayList; import java.util.List; -import java.util.Random; /** * This type extends UnOpenedProduct to support booster choice or random boosters @@ -43,7 +42,6 @@ public class UnOpenedMeta implements IUnOpenedProduct { private final List metaSets; private final JoinOperation operation; - private final Random generator = MyRandom.getRandom(); /** * Constructor for UnOpenedMeta. @@ -104,7 +102,7 @@ public class UnOpenedMeta implements IUnOpenedProduct { //$FALL-THROUGH$ case RandomOne: // AI should fall though here from the case above - int selected = generator.nextInt(metaSets.size()); + int selected = MyRandom.getRandom().nextInt(metaSets.size()); final IUnOpenedProduct newBooster = metaSets.get(selected).getBooster(); return newBooster.get(); diff --git a/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java b/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java index b0754024fee..36fb07057e8 100644 --- a/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java +++ b/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java @@ -23,6 +23,7 @@ import forge.quest.data.QuestPreferences.DifficultyPrefs; import forge.quest.data.QuestPreferences.QPref; import forge.quest.io.QuestDuelReader; import forge.util.CollectionSuppliers; +import forge.util.MyRandom; import forge.util.maps.EnumMapOfLists; import forge.util.maps.MapOfLists; import forge.util.storage.IStorage; @@ -230,11 +231,9 @@ public class QuestEventDuelManager { /** */ public void randomizeOpponents() { - final long seed = new Random().nextLong(); - final Random r = new Random(seed); for (QuestEventDifficulty qd : sortedDuels.keySet()) { List list = (List) sortedDuels.get(qd); - Collections.shuffle(list, r); + Collections.shuffle(list, MyRandom.getRandom()); } } diff --git a/forge-gui/src/main/java/forge/quest/io/ReadPriceList.java b/forge-gui/src/main/java/forge/quest/io/ReadPriceList.java index 4c97330134f..b8dc00929d4 100644 --- a/forge-gui/src/main/java/forge/quest/io/ReadPriceList.java +++ b/forge-gui/src/main/java/forge/quest/io/ReadPriceList.java @@ -25,7 +25,6 @@ import forge.util.MyRandom; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Random; /** *

@@ -73,7 +72,6 @@ public class ReadPriceList { private Map readFile(final String file) { final Map map = new HashMap<>(); - final Random r = MyRandom.getRandom(); final List lines = FileUtil.readFile(file); for (final String line : lines) { @@ -96,15 +94,15 @@ public class ReadPriceList { if (!(MagicColor.Constant.BASIC_LANDS.contains(name) || MagicColor.Constant.SNOW_LANDS.contains(name)) && !ForgeConstants.PRICES_BOOSTER_FILE.equals(file)) { float ff; - if (r.nextInt(100) < 90) { - ff = r.nextInt(10) * (float) .01; + if (MyRandom.getRandom().nextInt(100) < 90) { + ff = MyRandom.getRandom().nextInt(10) * (float) .01; } else { // +/- 50% - ff = r.nextInt(50) * (float) .01; + ff = MyRandom.getRandom().nextInt(50) * (float) .01; } - if (r.nextInt(100) < 50) { + if (MyRandom.getRandom().nextInt(100) < 50) { val = (int) (val * (1 - ff)); } else { // +ff% diff --git a/forge-gui/src/main/java/forge/sound/MusicPlaylist.java b/forge-gui/src/main/java/forge/sound/MusicPlaylist.java index f7a9f424e71..c5cbbb3d1ea 100644 --- a/forge-gui/src/main/java/forge/sound/MusicPlaylist.java +++ b/forge-gui/src/main/java/forge/sound/MusicPlaylist.java @@ -2,9 +2,9 @@ package forge.sound; import java.io.File; import java.io.FilenameFilter; -import java.util.Random; import forge.properties.ForgeConstants; +import forge.util.MyRandom; public enum MusicPlaylist { MENUS("menus/"), @@ -13,7 +13,6 @@ public enum MusicPlaylist { private final String subDir; private int mostRecentTrackIdx = -1; private String[] filenames; - private final Random randomGenerator = new Random(); private MusicPlaylist(String subDir0) { subDir = subDir0; @@ -44,7 +43,7 @@ public enum MusicPlaylist { else { //determine filename randomly from playlist int newIndex; do { - newIndex = randomGenerator.nextInt(filenames.length); + newIndex = MyRandom.getRandom().nextInt(filenames.length); } while (newIndex == mostRecentTrackIdx); //ensure a different track is chosen than the last one mostRecentTrackIdx = newIndex;