Write a program to generate any number of random integers in 0 to 100 range. Your program should get the size as a parameter and return the numbers as an array.
This the question I got and below is the code I tried, but I get duplicates with this code. How to generate random numbers without any duplicates?
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("size:\t");
int n = sc.nextInt();
int[] arr = new int[n];
Random rand = new Random(); //instance of a random class.
int upperbound = 101; //generate random values from 0-100
for(int i=0;i<n;i++) {
arr[i] = rand.nextInt(upperbound);
}
System.out.println("Random Numbers: "+ Arrays.toString(arr));
}
}