How to generate n random numbers from a list of m numbers in java?
For example I have a list of 20 numbers and i try to rand开发者_开发问答om generate six of them without repeating them. Any ideas?
If you have a java.util.List you could simple shuffle it and pick the first six.
An easy way is to randomly shuffle the list and then take the first six elements:
List<Number> population = ...your list of 20 numbers...
Collections.shuffle(population);
List<Number> sample = population.subList(0, 6);
加载中,请稍侯......
精彩评论