Math Random Java OR java.lang.Math.random() returns double type number. A value of this number is greater than or equal to 0.0 and less than 1.0. Where Returned values are chosen pseudorandomly with uniform distribution from that range.
A new pseudorandom-number generator, when the first time random() method called. After it is used thereafter for all calls to this method and is used nowhere else.
Why need Random numbers in java? The random number can use many applications and different types. And because a lot of applications are built-in Java it needs Java methods. For example, generating an OTP for logging or forgot a password is can use a random number method with fixed digits (4, 5, 6, etc).
Method Syntax
A method signature.
public static double random()
Returns value
The Java random() method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
Math random Java Example
import
java.lang.Math;
An output of the Random double type number value will be different every time of execution. So it will not match your output with this tutorial output.
import java.lang.Math; public class Hello { public static void main(String args[]) { // Generate random number double double rand = Math.random(); // Output will be different every time System.out.println("Random Number:" + rand); } }
Output: Random Number:0.5275968996954599
Java math random int
You can do it by multiplication of 100 and type casting value to int.
Formula – int rand = (int)(Math.random()*100);
import java.lang.Math; public class Hello { public static void main(String args[]) { // Generate random number int int rand = (int)(Math.random()*100); // Output will be different every time System.out.println("Random int Number is:" + rand); } }
Output: Random int Number is:43
Java Random nextint
Java Random nextInt() is given the next random integer value from the random number generator’s sequence.
It comes under util package – import Java.util.Random
Note: import java.lang.Math is different.
import java.util.Random; public class Hello { public static void main(String args[]) { // create random object Random ran = new Random(); // generating integer int nxt = ran.nextInt(); // Printing the random Number System.out.println("The Next Random integer is : " + nxt); } }
Output: The Next Random integer is: 21360465m
How to generate random numbers in java within range?
Answer: using java.util.concurrent.ThreadLocalRandom class you can get the random number within the range. See the below example code ho to do it.
Note: Doing this task with the java math random method is difficult but if you want then follow this link – How to get a random number in a range using Math.random()
import java.util.concurrent.ThreadLocalRandom; public class RandomNumbers { public static void main(String[] args) { int randomNo = ThreadLocalRandom.current().nextInt(1, 20); System.out.println("A Random number between 1 to 20 is: " + randomNo); } }
Output: A Random number between 1 to 20 is: 7
How to use math.random java formula
Answer:Math.random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Any Formula is depended on what you want to accomplish. If you want to have Numbered from 1 to 100 then its formula will be this-
(int)(Math.random()*100)
And if you want a range of values. Like random number range to 20 to 120, you have to add +20 at the end. See below code formula.
(int)(Math.random()*range) + min
Follow this link for more examples- Random Number Generator Java | Within Range | 5 Digit | Examples
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Math Random Java API examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.