samedi 4 décembre 2021

Unknown Amount of Random Objects with a value 0 - 1 - get one of them respecting their 0 - 1 value like a rarity [JAVA]

I'm currently writing a project where I need to get a random object respecting a rarity saved in each object with a method like this:

public Item getRandomItem() {
return ?
}

The 0-1 Value is stored in every Item (Items are in a Set<>):

Item#getRarity()

So how can I get a random Item respecting the "rarity" in the "Item" so that for example if there is only one Item in the Set<Item> with the rarity "0.4" it would be given to 100% and if there are two entries with the same rarity there would have a change 50% each and if there would be another two entries with rarities "0.1" and "0.9" there would have 10 and 90% chance. My problem is that all the rarities together are not 1 (100%) so it is possible that all the rarities together are higher or lower than the Number "1".

I hope you understand my problem and you have an idea how to solve it.

Thanks




Aucun commentaire:

Enregistrer un commentaire