如何制作一个随机数,但它不会产生相同的数字 [英] How to make a random number but it doesn't produce the same number

查看:122
本文介绍了如何制作一个随机数,但它不会产生相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用随机数生成器对整数进行随机排列

例如,我需要从1到30创建一个随机数。但它不会产生相同的随机数。产生的数量必须彼此不同。

For example I need to make a random number from 1 to 30. But it doesn't produce the same random number. The number produced has to be different one to another.

这可能吗?

谢谢

推荐答案

创建一个列表< Integer> ,其中包含1-30,使用<$对其进行随机播放c $ c> Collections.shuffle(),然后遍历列表。当然,在列表的末尾,不可能有一个尚未生成的新数字。

Create a List<Integer> containing 1-30, shuffle it using Collections.shuffle(), then iterate through the list. Of course, at the end of the list, it's impossible to have a new number that hasn't been generated yet.

这篇关于如何制作一个随机数,但它不会产生相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆