Код: Выделить всё
public List classes = Arrays.asList(
Collections.singletonMap(0, new Rarity("rarity_default", "Default!", 55.0)),
// ...
);
Я хочу получить случайный класс редкости из этих классов, но получил его случайно. из этого.
Например, у меня есть такие шансы:
- rarity_A: 55,00%
- rarity_B: 35,00%
- rarity_C: 12,00%
- rarity_D: 6,00%
- rarity_E : 3.00%
- rarity_F: 0.10%
В настоящее время вот мой код:
Код: Выделить всё
private final Map classes = new HashMap();
private final Random random = new Random();
public MobKillListener() {
for (Map rarityMap : SchemePlugin.getInstance().getSchemeConfig().classes) {
classes.putAll(rarityMap);
}
}
@EventHandler
public void onMobKill(EntityDeathEvent event) {
if (!(event.getEntity().getKiller() instanceof Player))
return;
Map rarities = new HashMap();
for (Map.Entry entry : classes.entrySet()) {
rarities.put(entry.getValue(), entry.getValue().rarity); // i need to remake the map
}
Rarity rarity = random(rarities);
if (rarity != null) {
// do my stuff
}
}
// i don't want to see this function ever again, this made me so mad..
public T random(Map map) {
double rand = (double) new Random().nextInt(10000001) / 100000.0D;
double total = 0.0D;
for (T t : map.keySet()) {
double chance = map.get(t) / 4.0D;
total += chance;
if (total >= rand) {
System.out.println("(" + chance + ") " + "Got " + total + ", rand: " + rand);
return t;
}
}
System.out.println("Got " + total + ", rand: " + rand);
return null;
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... age-chance