dimanche 3 mars 2019

Generating multiple random double numbers in a range, with Java?

I have done some research but I cannot find a working way for me to generate 9 random doubles in a range of (0 - 0.75).

I also need to add all 9 random numbers together for further calculations. I'm using DrJava.

So far from what I have tried, I only get one random number that repeats 9 times.

    double minLight = 0.0; 
    double maxLight = 0.75; 
    double totalSum = 0;

    for (int i = 0; i < 9; i++) {
    double monday = (Math.random() * (maxLight - minLight) + minLight);
    double mRounded = Math.round(monday * 100.0) / 100.0;
    totalSum += mRounded;  
    }

    double mWalk = totalSum + shortestWalk;

    for (int i = 0; i < 9; i++) {
    double wednesday = (Math.random() * (maxLight - minLight) + minLight);
    double wRounded = Math.round(wednesday * 100.0) / 100.0;
    System.out.println(wRounded);
    totalSum += wRounded;
    }

    double wWalk = totalSum + shortestWalk;         
    System.out.println(mWalk);
    System.out.println(wWalk);

Out Printed:
0.04
0.05
0.52
0.72
0.59
0.05
0.73
0.15
0.38
6.287142857142857
9.517142857142856




Aucun commentaire:

Enregistrer un commentaire