How to Generate Random Number in Java

How to Generate Random Number

A random number is a value that is created without any pattern or order.

Here you will learn that, How to Generate Random Number program in Java using two different ways. To generate Random numbers list, we will use random() method in Java.

Program 1 – Generate Random Number

Output

C:\CodeRevise\java>javac RandomNumber.java

C:\CodeRevise\java>java RandomNumber
Random Number: 82

C:\CodeRevise\java>java RandomNumber
Random Number: 50

C:\CodeRevise\java>java RandomNumber
Random Number: 84

C:\CodeRevise\java>java RandomNumber
Random Number: 9

Program 2 – How to Generate n Random numbers

Output

C:\CodeRevise\java>javac RandomNumber.java

C:\CodeRevise\java>java RandomNumber
How many Random number you want to generate : 7
Enter upper limit : 100
7 Random Numbers are :
42
53
99
4
61
1
97

 

 

Check out our other Java Programming Examples

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top