在java中生成随机数列表 [英] Generating a list of random numbers in java
本文介绍了在java中生成随机数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我生成一个随机数0或1
I generate a random number 0 or 1
int randomColor =(Math.random()< 0.5)? 0:1;
我需要创建52个随机数,其中26个为0,26个为1
I need to create 52 random numbers and 26 of them will be 0 and 26 are 1
推荐答案
您可以这样做:创建52个数字的列表
。用26个零和26个填充它,然后使用 Collections.shuffle()
以随机顺序对它们进行随机播放。
You can do this: Create a List
of 52 numbers. Fill it with 26 zeroes and 26 ones, and then use Collections.shuffle()
to shuffle them in a random order.
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 26; i++) {
numbers.add(0);
numbers.add(1);
}
Collections.shuffle(numbers);
这篇关于在java中生成随机数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文