How to randomize the given number without dublication in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to randomize the given number without dublication in Java?

How to randomize the given number without dublication? I need the algorithm of the randomizing the entered numbers with no repetition. For example: select 20 numbers from 1 to 100 without duplication.

1st Apr 2020, 9:43 AM
Bog'ibek Matyaqubov
Bog'ibek Matyaqubov - avatar
1 Answer
+ 5
You can use set for storing unique values only. And you can do just like this, Random rand =new Random(); LinkedHashSet set =new LinkedHashSet(); while (set.size() != 20) { set.add(rand.nextInt(100) + 1); } for (Integer s: set) System.out.println(s); }
1st Apr 2020, 9:51 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar