如何只生成一次1到100的随机数?

时间:2013-09-10 10:09:05

标签: actionscript-3 random numbers

我需要在AS3中生成1到100的随机数,这个数字不会生成两次。所以我需要生成每个数字,直到所有数字都完整。我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

使用数字1到100填充数组。

随机改组(使用Fisher-Yates shuffle)。

从第一个数组索引开始取每个数字......

答案 1 :(得分:1)

使用数字1-100填充数组'_randomNumbers'。每次需要号码时都要使用以下内容:

if (_randomNumbers.length>0) {
newRandomNumber = _randomNumbers.splice( Math.floor(Math.random(_randomNumbers.length)), 1 )[0];
}

答案 2 :(得分:0)

结帐this for more detail

 class NonRepeatedPRNG {
private final Random rnd = new Random();
private final Set<Integer> set = new HashSet<>();
public int nextInt() {
for (;;) {
  final int r = rnd.nextInt();
  if (set.add(r)) return r;
}
}
}
相关问题