mardi 25 juillet 2017

Obtaining random double with inclusive max value (Java - ThreadLocalRandom)

I have a method to obtain a random double between a min and a max value. My problem is that if the values are for example 0D and 10D I've never obtain 10D as a posible result because nextDouble second parameter is exclusive.

public static Double randomDouble(Double min, Double max) {
    return ThreadLocalRandom.current().nextDouble(min, max);
}

I've obtain a 10D result if I put this line in my method but I don't know if this is a good practice.

public static Double randomDouble(Double min, Double max) {

    max = max + 0.000000000000001D;

    return ThreadLocalRandom.current().nextDouble(min, max);
}

Is there another solution for this issue?




Aucun commentaire:

Enregistrer un commentaire