Generating random number within a limit without duplicates

Description

RandFunction.java file provides the random number list within a limit without encoutering duplicates in it. Random object availiable in util package is utilized for generating random number. Set interface is utilized for avoiding duplicates. Random number limit is given as input.

  • Filename: RandFunction.java

import java.util.*;
import javax.swing.*;
class RandFunction {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>();
int m=0;
String scount = JOptionPane.showInputDialog(null,"Enter random number limit");
int count=Integer.parseInt(scount);
Integer[] f = new Integer[count];
while(m<count) {
Random r = new Random();
int i = r.nextInt(count);
boolean valid = s.add(i);
if(valid) {
f[m]=i;
m++;
}
}

for(int k=0;k<count;k++) {
System.out.println(f[k]);
}
}
}