生成2个数字之间的唯一的随机数

编程入门 行业动态 更新时间:2024-10-09 15:17:16
本文介绍了生成2个数字之间的唯一的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有关我的具体目的,我需要之间产生整数1和120包容性(这是我可以做的没问题)。

For my specific purposes, I need to generate a whole number between 1 and 120 inclusive (which I can do no problem).

在该号码已经产生,我需要从池中拉它,因此它不能被重新生成。

Once that number has been generated, I need to pull it from the pool so it can't be generated again.

继续这个过程,直到所有的数字都用尽,此时,池填满备份,我们重新开始。

Continue this process until all numbers are exhausted, at which point, the pool fills back up and we start over again.

我怎么能去这样做?

推荐答案

生成编号从1到120的整个列表随机播放列表。就拿第一个元素(和删除)

Generate the whole list of numbers from 1 to 120. Shuffle the list. Take the first element (and remove it)

List<Integer> list = new LinkedList<Integer>(); for (int i = 1; i <= 120; i++) { list.add(i) } Collections.shuffle(list); ... int random = list.remove(0); ... int otherRandom = list.remove(0);

为您在你用完了数字的情况下,list.empty()。如果为空,再创建列表,并将它洗。

Check for list.empty() in case you run out of numbers. If empty, create the list again and shuffle it.

更多推荐

生成2个数字之间的唯一的随机数

本文发布于:2023-11-30 15:41:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:随机数   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!