非重复随机数组 [英] Non repeating random number array

查看:100
本文介绍了非重复随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个典型的整数填充数组,其中包含从0到20的10个随机非重复数字。另外,我需要能够修改它,这样我就可以从0到20中排除一些随机数。

I need to make a typical integer filled array with 10 random non repeating numbers from 0 to 20. Also, I need to be able to modify this so I can exclude some random numbers from 0 to 20.

我该怎么做?

推荐答案

您可以通过三个简单的步骤完成此操作:

You can do this in three easy steps:


  1. 使用您想要的所有候选编号建立一个列表

  2. 使用 Collections.shuffle 将该列表随机播放

  3. 使用该洗牌列表的 n 第一个元素。

  1. Build a list with all the candidate numbers you want
  2. Use Collections.shuffle to shuffle that list
  3. Use the n first elements of that shuffled list.

这篇关于非重复随机数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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