如何从给定的数字列表中使用Java生成随机数

时间:2022-11-25 19:56:53

Assume I have an array/vector of numbers like 1,3,7,9 then I need to guess a number from this list randomly. Using Random class in Java it seems like not possible to do this. Could anyone kindly help me to tell a way to do this kind of thing. I have to change the list of numbers used to generate random number. I am trying to implement a strategy to play battleship game automatically as an assignment. Kindly help me to do this ?

假设我有一个数字/向量的数字,如1,3,7,9然后我需要随机猜测这个列表中的数字。在Java中使用Random类似乎不可能这样做。任何人都可以帮助我说出做这种事情的方法。我必须更改用于生成随机数的数字列表。我正在尝试实施一种战略,作为任务自动发挥战舰游戏。请帮我这样做?

3 个解决方案

#1


4  

Put the numbers in an ArrayList and use Collections.shuffle(arrayList);

将数字放在ArrayList中并使用Collections.shuffle(arrayList);

#2


18  

If you just want to select one random number only, or want to select multiple random numbers with reinsertion (i.e. allow possibility of selecting the same number multiple times), you can generate a random index:

如果您只想选择一个随机数,或者想要使用重新插入选择多个随机数(即允许多次选择相同数字的可能性),您可以生成随机索引:

List<Integer> lst = ....;
int index = new Random().nextInt(lst.size());
Integer randomeValue = lst.get(index);

You can use an array instead as well. This requires O(1) for each selection.

您也可以使用数组。每次选择都需要O(1)。

If you need to select multiple distinct random numbers from the list, then using Collections.shuffle() and iterating through the list would be a better solution. This requires O(n) for all the queries.

如果您需要从列表中选择多个不同的随机数,那么使用Collections.shuffle()并遍历列表将是一个更好的解决方案。这需要所有查询的O(n)。

#3


0  

I'm with tordek on this one: Doesn't shuffling seem like a fairly heavy-weight way to select a configured number of random numbers from a source vector?

我正在使用tordek:不是洗牌似乎是一种从源向量中选择配置数量的随机数的相当重的方法吗?

Wouldn't it be faster to just take msaeed's suggestion for how to pick one random number, and repeat it n times? Perhaps assemble your random values as a Set, and keep selecting until your set size is big enough... (don't forget some kind of a check for the edge condition where there's insufficient numbers in the source vector to supply the configured number of random values)

如果采取msaeed的建议如何选择一个随机数并重复n次,难道不是更快吗?也许将你的随机值组合成一个Set,并继续选择直到你的设置大小足够大......(不要忘记对源边向量中数量不足的边缘条件进行某种检查以提供配置的数量随机值)

#1


4  

Put the numbers in an ArrayList and use Collections.shuffle(arrayList);

将数字放在ArrayList中并使用Collections.shuffle(arrayList);

#2


18  

If you just want to select one random number only, or want to select multiple random numbers with reinsertion (i.e. allow possibility of selecting the same number multiple times), you can generate a random index:

如果您只想选择一个随机数,或者想要使用重新插入选择多个随机数(即允许多次选择相同数字的可能性),您可以生成随机索引:

List<Integer> lst = ....;
int index = new Random().nextInt(lst.size());
Integer randomeValue = lst.get(index);

You can use an array instead as well. This requires O(1) for each selection.

您也可以使用数组。每次选择都需要O(1)。

If you need to select multiple distinct random numbers from the list, then using Collections.shuffle() and iterating through the list would be a better solution. This requires O(n) for all the queries.

如果您需要从列表中选择多个不同的随机数,那么使用Collections.shuffle()并遍历列表将是一个更好的解决方案。这需要所有查询的O(n)。

#3


0  

I'm with tordek on this one: Doesn't shuffling seem like a fairly heavy-weight way to select a configured number of random numbers from a source vector?

我正在使用tordek:不是洗牌似乎是一种从源向量中选择配置数量的随机数的相当重的方法吗?

Wouldn't it be faster to just take msaeed's suggestion for how to pick one random number, and repeat it n times? Perhaps assemble your random values as a Set, and keep selecting until your set size is big enough... (don't forget some kind of a check for the edge condition where there's insufficient numbers in the source vector to supply the configured number of random values)

如果采取msaeed的建议如何选择一个随机数并重复n次,难道不是更快吗?也许将你的随机值组合成一个Set,并继续选择直到你的设置大小足够大......(不要忘记对源边向量中数量不足的边缘条件进行某种检查以提供配置的数量随机值)