There are two classes that you can make use of to generate random numbers with a specified range,
- java.util.Random (added in Java 8)
- jmath.Random
Let us see an example to generate random numbers between 1 to 100.
Example 1: Random Numbers using Java 8 java.util.Random
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class JavaRamdonNumbersExample {
public static void main(String[] args) {
List<Integer> randomNumbers = new ArrayList<>();
Random randomRange0to100 = new Random();
for(int i = 0; i <=10; i++) {
randomNumbers.add((int) randomRange0to100.nextInt(100));
}
System.out.println(randomNumbers);
}
}
Output:
First Execution: [53, 44, 45, 28, 58, 53, 61, 91, 75, 6, 56]
Second Execution: [82, 57, 47, 97, 96, 66, 34, 26, 31, 37, 95]
Third Execution: [18, 84, 64, 74, 76, 4, 38, 24, 31, 11, 4]
Example 2: Random Numbers using jmath.Random
import java.util.ArrayList;
import java.util.List;
public class JavaRamdomNumbersExample {
public static void main(String[] args) {
List<Integer> randomNumbers = new ArrayList<>();
for(int i = 0; i <=10; i++) {
randomNumbers.add((int) (Math.random() * 100 + 1));
}
System.out.println(randomNumbers);
}
}
Output:
First Execution: [55, 28, 60, 84, 70, 91, 39, 93, 20, 17, 61]
Second Execution: [37, 27, 60, 77, 64, 7, 33, 6, 12, 96, 30]
Third Execution: [3, 100, 65, 59, 13, 86, 26, 51, 13, 74, 59]
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
- Get cURL command from Chrome Inspect Network HTTP URL - cURL
- Java 8 JDBC: Insert Timestamp Code Example - Java
- Fix - Microsoft Windows Error Code: 0xA00F4244 NoCamerasAreAttached - Microsoft
- Change the default download location for Mac Safari - MacOS
- Remove ActionBar from Activity that extends appcompat-v7 - Android
- Solution: AWS S3 CLI Command AccessDenied - S3
- Delete file using PHP code : unlink() - PHP
- Create a Directory using Java Code - Java