Generate a random float in Java
This post will discuss how to generate a random float in Java.
1. Using Math.random() method
The standard solution to generate a pseudorandom double value is using the Math.random() method. It returns a uniformly distributed value within the range [0.0, 1.0), which represents greater than or equal to 0.0 and less than 1.0.
|
1 2 3 4 5 6 |
public class Main { public static void main(String[] args) { double random = Math.random(); System.out.println(random); } } |
You can easily extend the solution to any valid range [min, max], as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Main { private static double getRandom(int min, int max) { if (min >= max) { throw new IllegalArgumentException("Invalid range [" + min + ", " + max + "]"); } return min + Math.random() * (max - min); } public static void main(String[] args) { int min = 0; int max = 10; double random = getRandom(min, max); System.out.println(random); } } |
2. Using Random.nextDouble() method
Alternatively, you can use the Random.nextDouble() method to generate a pseudorandom uniformly-distributed double value between 0.0 and 1.0 with a random number generator (RNG). Note that, 0.0d is inclusive and 1.0d is exclusive.
|
1 2 3 4 5 6 7 8 9 |
import java.util.Random; public class Main { public static void main(String[] args) { Random r = new Random(); double random = r.nextDouble(); System.out.println(random); } } |
You may use the SecureRandom class, to get a cryptographically secure random number generator.
|
1 2 3 4 5 6 7 8 9 10 |
import java.security.SecureRandom; import java.util.Random; public class Main { public static void main(String[] args) { Random r = new SecureRandom(); double random = r.nextDouble(); System.out.println(random); } } |
To get the pseudorandom uniformly-distributed double value in range [min, max], do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.security.SecureRandom; import java.util.Random; public class Main { private static double getRandom(int min, int max) { if (min >= max) { throw new IllegalArgumentException("Invalid range [" + min + ", " + max + "]"); } Random r = new SecureRandom(); return min + r.nextDouble() * (max - min); } public static void main(String[] args) { int min = 0; int max = 10; double random = getRandom(min, max); System.out.println(random); } } |
3. Using Random.nextFloat() method
Similar to the nextDouble method, the Random class contains the corresponding methods for Integer, Long, Boolean, Float, etc. To get a pseudorandom float value between 0.0f (inclusive) and 1.0f (exclusive), you can use Random.nextFloat() method.
|
1 2 3 4 5 6 7 8 9 |
import java.util.Random; public class Main { public static void main(String[] args) { Random r = new Random(); float random = r.nextFloat(); System.out.println(random); } } |
As with other methods, you can easily extend the solution to work with any range:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Random; public class Main { private static double getRandom(int min, int max) { if (min >= max) { throw new IllegalArgumentException("Invalid range [" + min + ", " + max + "]"); } Random r = new Random(); return min + r.nextFloat() * (max - min); } public static void main(String[] args) { int min = 0; int max = 10; double random = getRandom(min, max); System.out.println(random); } } |
That’s all about generating a random float in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)