samedi 23 mai 2015

Java output from list based on chance

I'm trying to figure out a method which as two arguments, one is a Object[] and the other a float[], both same size. the floats are the chances of the output being the corresponding object in the object list, object in index i has chance (0.0-1.0) stored in the float in index i. The values on the chance list all sum up to 1.0 if all summed up together.

The thing that I want this method to do is to return one of the objects from the list randomly, being the chance of each object of being picked inside the chance list. Example {banana, apple, orange} {0.1, 0.5, 0.4} Having 10% chance of returning banana, 50% apple and 40% orange.

Note: The method can't be for a fixed number of objects, so a hard-coded if chain won't work :/

Any help is much appreciated.




Aucun commentaire:

Enregistrer un commentaire