Fixing some weird randomness issue of java.util.Random

This commit is contained in:
marcin.sciesinski
2019-09-20 12:56:39 -07:00
parent 55fb0ae130
commit bede97baf3
2 changed files with 31 additions and 0 deletions

View File

@@ -98,6 +98,8 @@ public class DraftChoiceBuilder {
private List<CardCollection.Item> getCards(long seed, int stage) {
Random rnd = getRandom(seed, stage);
// Fixing some weird issue with Random
float thisFixesRandomnessForSomeReason = rnd.nextFloat();
final List<CardCollection.Item> cards = new ArrayList<CardCollection.Item>(possibleCards);
Collections.shuffle(cards, rnd);
return cards;
@@ -213,6 +215,8 @@ public class DraftChoiceBuilder {
private List<String> getShuffledCards(long seed, int stage) {
Random rnd = getRandom(seed, stage);
// Fixing some weird issue with Random
float thisFixesRandomnessForSomeReason = rnd.nextFloat();
final List<String> shuffledCards = new ArrayList<String>(cards);
Collections.shuffle(shuffledCards, rnd);
return shuffledCards;
@@ -231,12 +235,16 @@ public class DraftChoiceBuilder {
@Override
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
Random rnd = getRandom(seed, stage);
// Fixing some weird issue with Random
float thisFixesRandomnessForSomeReason = rnd.nextFloat();
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getDraftChoice(seed, stage);
}
@Override
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
Random rnd = getRandom(seed, stage);
// Fixing some weird issue with Random
float thisFixesRandomnessForSomeReason = rnd.nextFloat();
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getCardsForChoiceId(choiceId, seed, stage);
}
};

View File

@@ -12,10 +12,13 @@ import com.gempukku.lotro.game.packs.SetDefinition;
import java.io.File;
import java.util.Comparator;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class HobbitDraftTest {
public static void main(String[] args) {
testRandomness();
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
final String property = System.getProperty("user.dir");
String projectRoot = new File(property).getAbsolutePath();
@@ -75,6 +78,26 @@ public class HobbitDraftTest {
}
}
private static void testRandomness() {
doRandomTest(false);
System.out.println("WTF!!!!");
doRandomTest(true);
}
private static void doRandomTest(boolean getFloatBeforeInt) {
System.out.println("Get float before int: " + getFloatBeforeInt);
int[] values = new int[4];
for (int i = 0; i < 1000; i++) {
Random rnd = new Random(i);
if (getFloatBeforeInt)
rnd.nextFloat();
values[rnd.nextInt(4)]++;
}
for (int i = 0; i < values.length; i++) {
System.out.println(i + ": " + values[i]);
}
}
private static long getSeed(String collectionType, int playerId) {
return collectionType.hashCode() + playerId * 8963;
}