I need to generate a random number from 1 to 100 in AS3 that will not be generated twice. So I need every number be generated until all the numbers are complete. How can I do that?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Fill an array with the numbers 1 to 100.
Randomly shuffle it (use Fisher-Yates shuffle).
Take each number starting from the first array index onwards...
回答2:
Fill an Array '_randomNumbers' with the numbers 1-100. Each time you need a number use the following:
if (_randomNumbers.length>0) {
newRandomNumber = _randomNumbers.splice( Math.floor(Math.random(_randomNumbers.length)), 1 )[0];
}
回答3:
check out 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;
}
}
}