mercredi 18 octobre 2017

Java Random() rounding off

I'm using Java's Random to generate random numbers: 1.0, 1.1 - 10

    Random random = new Random();
    return (double) ((random.nextInt(91) + 10) / 10.0);

When I printed a lot of these numbers (2000) I noticed 1.0 and 10 are significant less printed then all others (repeated 20 times, happened every time). Most likely because 0.95-0.99 and 10.01-10.04 aren't generated.

Now I have read a lot of threads about his, but it still leaves me to the following question:

If these numbers would represent grades for example, you can't get lower then a 1 and higher then a 10 here, would it be legit to extend the range from 0.95 up to 10.04?

    Random random = new Random();
    return Double.valueOf((1005-95) / 100);

Kind regards,

Richard




Aucun commentaire:

Enregistrer un commentaire