Last active
August 29, 2015 14:18
-
-
Save WouterG/925d986e6141cd76847a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Object getRandom(HashMap<Object, Integer> chanceMap) { | |
int chancey = 0; | |
for (int c : chanceMap.values()) { | |
chancey += c; | |
} | |
int index = myRandom.nextInt(chancey); | |
Object o = null; | |
for (Entry<Object, Integer> e : chanceMap.entrySet()) { | |
index -= e.value(); | |
if (index <= 0) { | |
o = e.key(); | |
break; | |
} | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment