how to create a function that can return a linkedList with 4 random nombers and those nombers dont be repeated ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to create a function that can return a linkedList with 4 random nombers and those nombers dont be repeated ?

thank for who while help me

18th Oct 2016, 1:39 AM
gol_roger
gol_roger - avatar
1 Answer
+ 1
Inside your method, create a LinkedList. Create a while-loop that runs while list.size() < 4 . random.nextInt() a fresh random number, and if(!list.contains(thatNumber)) , list.add(thatNumber) . Maybe you are better off using a Set (a HashSet), because uniqueness is the Set's Feature: you could skip the if-clause and just add stuff until the size of the set is good.
4th Nov 2016, 6:35 AM
Holger Müller
Holger Müller - avatar