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);
}
}
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!