Fixing some weird randomness issue of java.util.Random
This commit is contained in:
@@ -98,6 +98,8 @@ public class DraftChoiceBuilder {
|
|||||||
|
|
||||||
private List<CardCollection.Item> getCards(long seed, int stage) {
|
private List<CardCollection.Item> getCards(long seed, int stage) {
|
||||||
Random rnd = getRandom(seed, 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);
|
final List<CardCollection.Item> cards = new ArrayList<CardCollection.Item>(possibleCards);
|
||||||
Collections.shuffle(cards, rnd);
|
Collections.shuffle(cards, rnd);
|
||||||
return cards;
|
return cards;
|
||||||
@@ -213,6 +215,8 @@ public class DraftChoiceBuilder {
|
|||||||
|
|
||||||
private List<String> getShuffledCards(long seed, int stage) {
|
private List<String> getShuffledCards(long seed, int stage) {
|
||||||
Random rnd = getRandom(seed, stage);
|
Random rnd = getRandom(seed, stage);
|
||||||
|
// Fixing some weird issue with Random
|
||||||
|
float thisFixesRandomnessForSomeReason = rnd.nextFloat();
|
||||||
final List<String> shuffledCards = new ArrayList<String>(cards);
|
final List<String> shuffledCards = new ArrayList<String>(cards);
|
||||||
Collections.shuffle(shuffledCards, rnd);
|
Collections.shuffle(shuffledCards, rnd);
|
||||||
return shuffledCards;
|
return shuffledCards;
|
||||||
@@ -231,12 +235,16 @@ public class DraftChoiceBuilder {
|
|||||||
@Override
|
@Override
|
||||||
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
|
public Iterable<SoloDraft.DraftChoice> getDraftChoice(long seed, int stage) {
|
||||||
Random rnd = getRandom(seed, 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);
|
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getDraftChoice(seed, stage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
|
public CardCollection getCardsForChoiceId(String choiceId, long seed, int stage) {
|
||||||
Random rnd = getRandom(seed, 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);
|
return draftChoiceDefinitionList.get(rnd.nextInt(draftChoiceDefinitionList.size())).getCardsForChoiceId(choiceId, seed, stage);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,10 +12,13 @@ import com.gempukku.lotro.game.packs.SetDefinition;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class HobbitDraftTest {
|
public class HobbitDraftTest {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
testRandomness();
|
||||||
|
|
||||||
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
|
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
|
||||||
final String property = System.getProperty("user.dir");
|
final String property = System.getProperty("user.dir");
|
||||||
String projectRoot = new File(property).getAbsolutePath();
|
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) {
|
private static long getSeed(String collectionType, int playerId) {
|
||||||
return collectionType.hashCode() + playerId * 8963;
|
return collectionType.hashCode() + playerId * 8963;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user