Java 从 ArrayList 中获取随机数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19346965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:19:42  来源:igfitidea点击:

Get random number from ArrayList?

javamathrandomarraylist

提问by Juustosta L?ytyy

ArrayList<Integer>  lista = new ArrayList<Integer>();
lista.add(159);
lista.add(170);
lista.add(256);

For example, I got these 3 numbers in my arraylist and I want to randomly get one of them. How is that possible?

例如,我在我的数组列表中得到了这 3 个数字,我想随机得到其中一个。这怎么可能?

采纳答案by galovics

One way to do this to use the Random class:

使用 Random 类的一种方法:

ArrayList<Integer>  lista = new ArrayList<Integer>();
lista.add(159);
lista.add(170);
lista.add(256);

Random r = new Random();
System.out.println(lista.get(r.nextInt(lista.size())));

Or use shuffle:

或者使用随机播放:

ArrayList<Integer>  lista = new ArrayList<Integer>();
lista.add(159);
lista.add(170);
lista.add(256);

Collections.shuffle(lista);
System.out.println(lista.get(0));

回答by BobTheBuilder

Use random on number of elements in the list and used that as index.

对列表中的元素数量使用 random 并将其用作索引。

回答by Ravi Thapliyal

You can make use of Randomto generate an intto be used as a random index.

您可以利用Random生成一个int用作随机索引。

Random rand = new Random();
Integer randomInt = lista.get(rand.nextInt(lista.size()));

Here, rand.nextInt(lista.size())would generate a random index between 0and size - 1.

在这里,rand.nextInt(lista.size())会在0和之间生成一个随机索引size - 1

Reference:
Random#nextInt(int)

参考
随机#nextInt(int)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

返回一个伪随机、均匀分布的 int 值,该值介于 0(包含)和指定值(包含)之间,从该随机数生成器的序列中抽取。

回答by Mohammad Adnan

do this:

做这个:

Random rn = new Random();
int i = rn.nextInt() % lista.size();
System.out.println(lista.get(i));

回答by Bunny Rabbit

The idea is to generate a new random index and use that random index to get a radom number from the array.

这个想法是生成一个新的随机索引并使用该随机索引从数组中获取随机数。

For that

为了那个原因

  1. Genrate a random number using Math.random()which Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
  2. Adjust the random value to fall between the start index and the end index of the list. We do this by multiplying the double value with the size of the list and converting it to integer, because the list indices are integers.
  1. 生成一个随机数,使用Math.random()它返回一个带正号的双精度值,大于或等于 0.0 且小于 1.0。
  2. 调整随机值以落在列表的开始索引和结束索引之间。我们通过将 double 值乘以列表的大小并将其转换为整数来实现这一点,因为列表索引是整数。

This gives us the following code.

这给了我们以下代码。

int randomIndex = (int)(Math.random() * lista.size());

System.out.println(lista.get(randomIndex));

Please note that the random number generated here are not truely random and are generated using a psuedorandom generator, which suffices for most of the everyday uses.

请注意,这里生成的随机数并不是真正的随机数,而是使用伪随机生成器生成的,足以满足大多数日常使用。

You can read more about PRNGhere if you are curious :)

如果您好奇,可以在此处阅读有关PRNG 的更多信息:)