If you just want to select only one random number or want to select several random numbers with reloading (i.e. allow the possibility to select the same number several times), you can create a random index:
List<Integer> lst = ....; int index = new Random().nextInt(lst.size()); Integer randomeValue = lst.get(index);
You can use an array instead. Each selection requires O(1)
.
If you need to select several different random numbers from a list, then using Collections.shuffle()
and repeating in the list would be the best solution. This requires O(n)
for all queries.
notnoop
source share