In this program, we will learn to code the Java Program to Generate Random Numbers. Let’s understand How we will generate random numbers in Java Programming Language?
In this Program, we will use two approaches to generate random numbers in Java,
- Math.Random method in Java – this generate any random value of double type.
- java.util.Random class in Java – Random class contains methods like nextInt(), nextDouble() which can generate random value.
Let’s see the Java Program to Generate Random Numbers using Math.random
method.
Java Program to Generate Random Numbers
import java.util.*; public class Main{ public static void main(String[] args){ // Generating Random Double value double random1 = Math.random(); double random2 = Math.random(); System.out.println("Random Number 1 : "+random1); System.out.println("Random Number 2 : "+random2); } }
Output
Random Number 1 : 0.24921997920987005
Random Number 2 : 0.39881323226439214
How this program works?
// Generating Random Double value double random1 = Math.random(); double random2 = Math.random();
In this program, we have used the Math.random()
method present in Math Class. This method generates any random value of double datatype. Then, we just print the random values generated by the Math.random()
method.
This is the Java Program to Generate Random Numbers using Math.random()
method.
Now, let’s see the other approach to generate random numbers. In this approach, we will use the Random Class of java.util
package.
Java Program to Generate Random Numbers
The Random class is present in the java.util
package.
Random Integer Value
import java.util.*; public class Main{ public static void main(String[] args){ // Creating instance of Random Class Random random = new Random(); int random1 = random.nextInt(100); // generate random values till 100 int random2 = random.nextInt(200); // generate random values till 200 int random3 = random.nextInt(11); // generate random values till 11 System.out.println("Random value till 100 : "+random1); System.out.println("Random value till 200 : "+random2); System.out.println("Random value till 11 : "+random3); } }
Output
Random value till 100 : 93
Random value till 200 : 56
Random value till 11 : 5
In this program, we used the nextInt()
function of Random Class which takes a number in the function as a parameter to generate a random number till the number which was passed as a parameter.
For example, random.nextInt(100)
will generate a random number till 100 and random.nextInt(200)
will generate a random number till 200.
Random Double Value
import java.util.*; public class Main{ public static void main(String[] args){ // Creating instance of Random Class Random random = new Random(); double random1 = random.nextDouble(); double random2 = random.nextDouble(); System.out.println("Random value (double) : "+random1); System.out.println("Random value (double) : "+random2); } }
Output
Random value (double) : 0.684892500972126
Random value (double) : 0.8893342329504045
In this program, we used the nextDouble()
function of Random Class to generate a random double. Note: we can’t pass any value as parameter unlike nextInt() function.
Conclusion
I hope after going through this post, you understand the Java Program to Generate Random Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read:
- Java Program to Add Two Complex Numbers
- Java Program to Multiply Two Numbers
- Java Program to Swap Two Numbers
- Java Program to Find Factorial of a Number
- Java Program to Check Prime Number