[英]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 random generate six of them without repeating them. 例如,我有一个包含20个数字的列表,我尝试随机生成其中六个而不重复它们。 Any ideas? 有任何想法吗?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.