Amazing technological breakthrough possible @S-Logix

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • +91- 81240 01111

Social List

How to generate random number in java without duplicates?

Description 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.

Sample Code
  • Filename:

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]); } } }


Generate random number in java without duplicates
Random number in java