How to generate random float numbers in a specific range in Java

1 Answer

0 votes
package javaapplication1;

import java.util.Random;

public class JavaApplication1 {

    public static void main(String[] args) {
     
        Random rand = new Random();

        float f;
        float min = 3.0f;
        float max = 31.0f;
        
        for (int i = 0; i < 15; i++) {
                f = rand.nextFloat() * (max - min) + min;
                System.out.format("%.2f\n", f);
            }
        }
    }
 
/*

run:

30.00
8.24
20.65
10.91
26.36
5.84
21.08
19.10
9.79
28.03
13.69
16.02
9.52
25.55
22.86

*/

 



answered Oct 9, 2016 by avibootz

Related questions

1 answer 93 views
1 answer 132 views
1 answer 134 views
1 answer 99 views
1 answer 117 views
1 answer 101 views
...