dimanche 26 avril 2020

Create an Object based on Probability

I'm creating Objects in Java, So I have 4 classes and each has their respective probabilities eg 50%, 30%, 20% and the last object has 0%.

These probabilities will change over time and the last object won't stay at 0%.

They will always add up to 100% and never go over.

I need to be able to choose an object based on their probabilty.

The only way I could think of so far was to create a list of 100 elements and populate with 50 of one object, 30 of another, and 20 of another. Then get a random integer between 0 and 100. But this isn't very efficient especially since I will be calling this multiple times.

Can anyone advise on a method that doesn't use anything outside of Java's inbuilt packages and functionality.




Aucun commentaire:

Enregistrer un commentaire