dimanche 12 mars 2017

Get a random object from a list of percentages

I can't seem to find an effective way of choosing a random object by a percentage to each object.

Basically, I give the method a map of objects, and a chance of that object being picked. Preferably I want it so it can have has many decimals as the longest one has, but 1 decimal is fine too.

public static <T> T random(Map<T, Double> chances) {
    T toReturn = null;
    double rand = new Random().nextInt(1000) / 10.0D;

    // ???

    return toReturn;
}

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire