To generate random numbers in a given range, the Java code is as follows −
Example
import java.util.Random;
import java.util.*;
public class Demo{
public static void main(String args[]){
Random my_rand = new Random();
List my_list_1 = new ArrayList();
int v_1 = my_rand.nextInt(1000);
int v_2 = my_rand.nextInt(967);
int v_3 = my_rand.nextInt(1050);
int v_4 = my_rand.nextInt(10000);
int v_5 = my_rand.nextInt(100);
my_list_1.add(v_1);
my_list_1.add(v_2);
my_list_1.add(v_3);
my_list_1.add(v_4);
my_list_1.add(v_5);
System.out.println("The random values in the list are : ");
for(int i=0; i<my_list_1.size(); i++)
System.out.print(my_list_1.get(i)+" ");
}
}Output
The random values in the list are : 677 230 763 1695 48
A class named Demo contains the main function. Here, a new random instance is created along with a new array list. Random elements are created and assigned to variables. These random variables are added to the list using the ‘add’ function. These elements are displayed on the console.