Skip to content

Instantly share code, notes, and snippets.

@WouterG
Last active August 29, 2015 14:18
Show Gist options
  • Save WouterG/925d986e6141cd76847a to your computer and use it in GitHub Desktop.
Save WouterG/925d986e6141cd76847a to your computer and use it in GitHub Desktop.
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