javarandomduplication

prevent repetition of the numbers by not use of Collection Shuffle [Java]


I used random function in my code however i dont know how could i prevent repetition.Also i read another questions about this subject but i dont want to use "Shuffle".

Thanks for all help and clues.


Solution

  • Set doesn't allow duplicates

    Set set = new HashSet();
    
    while (set.size() < 20) {
        set.add(r.nextInt(100));
    }