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.
This commit is contained in:
Meerkov
2018-04-16 21:26:12 -07:00
parent 645d70e6ce
commit 5c9a27c930
21 changed files with 81 additions and 102 deletions

View File

@@ -27,7 +27,6 @@ import forge.util.MyRandom;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* <p>
@@ -120,7 +119,6 @@ public class DeckGeneratorTheme extends DeckGeneratorBase {
final List<Grp> 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++;
}

View File

@@ -43,10 +43,9 @@ import java.util.*;
public class DeckgenUtil {
public static Deck buildCardGenDeck(GameFormat format, boolean isForAI){
Random random = new Random();
try {
List<String> 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<PaperCard> 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<PaperCard> preSelectedCards = new ArrayList<>();
for(Map.Entry<PaperCard,Integer> pair:potentialCards){
preSelectedCards.add(pair.getKey());
@@ -146,7 +144,7 @@ public class DeckgenUtil {
if(preSelectedCards.size()<randMax){
randMax=preSelectedCards.size();
}
PaperCard secondKeycard = preSelectedCards.get(r.nextInt(randMax));
PaperCard secondKeycard = preSelectedCards.get(MyRandom.getRandom().nextInt(randMax));
List<Map.Entry<PaperCard,Integer>> 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<PaperCard> 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;j<keyCardCount;++j) {
playsetList.add(card);
@@ -194,16 +192,16 @@ public class DeckgenUtil {
//Add 2nd keycard
int keyCard2Count=4;
if(card.getRules().getMainPart().getManaCost().getCMC()>7){
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<keyCard2Count;++j) {
playsetList.add(secondKeycard);
}
for (PaperCard c:selectedCards){
for(int j=0;j<4;++j) {
if(r.nextInt(100)<90) {
if(MyRandom.getRandom().nextInt(100)<90) {
playsetList.add(c);
}
}
@@ -525,7 +523,6 @@ public class DeckgenUtil {
}else {
potentialCards.addAll(CardRelationMatrixGenerator.cardPools.get(DeckFormat.Commander.toString()).get(commander.getName()));
}
Random r = new Random();
//Collections.shuffle(potentialCards, r);
List<PaperCard> preSelectedCards = new ArrayList<>();
for(Map.Entry<PaperCard,Integer> 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<PaperCard> cardList = Lists.newArrayList(colorList);
Collections.shuffle(cardList, new Random());
Collections.shuffle(cardList, MyRandom.getRandom());
int shortlistlength=400;
if(cardList.size()<shortlistlength){
shortlistlength=cardList.size();

View File

@@ -514,7 +514,7 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase {
}
};
List<PaperCard> randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
Collections.shuffle(randomPool,new Random());
Collections.shuffle(randomPool, MyRandom.getRandom());
Iterator<PaperCard> 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<PaperCard> randomPool = Lists.newArrayList(pool.getAllCards(possibleFromFullPool));
Collections.shuffle(randomPool,new Random());
Collections.shuffle(randomPool, MyRandom.getRandom());
Iterator<PaperCard> iRandomPool=randomPool.iterator();
for(int i=0;i<num;++i){
PaperCard randomCard=iRandomPool.next();
@@ -801,12 +801,12 @@ public class CardThemedDeckBuilder extends DeckGeneratorBase {
rankedColorList.removeAll(keyCardList);
}*/
final Map<Integer,Integer> 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<Integer, Integer> creatureCosts = new HashMap<Integer, Integer>();

View File

@@ -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);
}

View File

@@ -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<MetaSet> 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();

View File

@@ -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<QuestEventDuel> list = (List<QuestEventDuel>) sortedDuels.get(qd);
Collections.shuffle(list, r);
Collections.shuffle(list, MyRandom.getRandom());
}
}

View File

@@ -25,7 +25,6 @@ import forge.util.MyRandom;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* <p>
@@ -73,7 +72,6 @@ public class ReadPriceList {
private Map<String, Integer> readFile(final String file) {
final Map<String, Integer> map = new HashMap<>();
final Random r = MyRandom.getRandom();
final List<String> 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%

View File

@@ -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;