package org.code2care.example;
import java.util.Random;
public class JavaRandomNumberGeneratorExample {
public static void main(String[] args) {
int noOfRandomNumbers = 10;
generateRandomNumbers(noOfRandomNumbers);
}
public static void generateRandomNumbers(int noOfRandomNumbers) {
Random random = new Random();
for (int i = 0; i < noOfRandomNumbers; i++) {
int randomNumber = random.nextInt();
System.out.println(randomNumber);
}
}
}
Output:
-2125433095
836305817
-896405979
-1748201265
1330120858
20769789
1066517992
890512865
427376433
1248883616
Example 2: Generate random numbers between -100 to 100
public static void generateRandomNumbers(int noOfRandomNumbers) {
Random random = new Random();
for (int i = 0; i < noOfRandomNumbers; i++) {
int randomNumber = random.nextInt(-100,100);
System.out.println(randomNumber);
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to remove username from Mac Menu Bar? - MacOS
- Setting JAVA_HOME in macOS Big Sur - MacOS
- TextEdit Get the count of lines in a file - MacOS
- Fix: Microsoft Teams Error Code 2:- 1200 - Teams
- Make Android View Scrollable both Horizontally and Vertically - Android
- Fix Microsoft Teams Error code - 107 - Teams
- How to run Java Unit Test cases with Apache Maven? - Java
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle. - Java