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]
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Calculate Volume of Pyramid - C-Program
- How to Install glib on Mac - MacOS
- Notepad++ copy above line-example - NotepadPlusPlus
- How to fix java.net.NoRouteToHostException in Android Studio - Android-Studio
- Fix: Unable to edit text in TextEdit on Mac - MacOS
- How to Know the Build Version Details of Microsoft Office 365 Applications - Microsoft
- Deep Dive: Java Object Class from java.lang Package - Java
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years - Java