java 如何使用 r.nextInt() 除了一个数字生成随机数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16265890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate random numbers with r.nextInt() except one number?
提问by Hayden Perry
I've come to a part in my code where I must generate a random number every time I press a button (r in this case). When I press this button, I want it to generate a number between 0 and n (3 in this case), but I don't want it to generate a number it previously generated. So I do not want the same number to be generated twice in a row. So 2 then 2 is bad. 2 then 0 then 2 is OK however.
在我的代码中,我必须在每次按下按钮时生成一个随机数(在本例中为 r)。当我按下此按钮时,我希望它生成一个介于 0 和 n 之间的数字(在本例中为 3),但我不希望它生成一个先前生成的数字。所以我不希望连续生成两次相同的数字。所以2然后2是坏的。2 然后 0 然后 2 是可以的。
I've looked around on here for questions similar to mine but none of them have really helped. Everyone else is generating once with the exception of numbers in an array or something. I'm constantly generating and I want to be able to detect the same number previous.
我在这里四处寻找与我类似的问题,但没有一个真正有帮助。除了数组中的数字或其他东西之外,其他所有人都生成一次。我一直在生成,我希望能够检测到以前的相同数字。
I am using the Random class, I have considered using the math.random class but that is between 0 and 1 so that isn't really too useful. Any help would be greatly appreciated, thanks! :D
我正在使用 Random 类,我考虑过使用 math.random 类,但它介于 0 和 1 之间,所以这并不是很有用。任何帮助将不胜感激,谢谢!:D
回答by tianz
Memorize what you generated last time; repeat generating until they are different
记住上次生成的内容;重复生成直到它们不同
Say you want numbers 0-9
假设你想要数字 0-9
do
{
int n = Random.nextInt(10);
} while (n == prev) // prev is the number you generated previously
prev = n;
回答by Mike Samuel
Since you have n possible values for the first, and only n-1 for the subsequent, just use randInt
with a different argument depending on whether you're producing the first value or not. Trying to use randInt
with the same arguments for all iterations will result in a non-flat distribution.
由于第一个值有 n 个可能值,随后的值只有 n-1 个,因此只需randInt
根据您是否生成第一个值使用不同的参数即可。尝试randInt
对所有迭代使用相同的参数将导致非平坦分布。
class NoAdjacentPRNG implements Iterator<Integer> {
private final Random rnd;
private final int range; // 3 to generate numbers in [0, 2).
private Integer last;
NoAdjacentPRNG(Random rnd, int range) {
this.rnd = rnd;
this.range = range;
}
public boolean hasNext() { return true; }
public Integer next() {
int n;
if (last == null) {
// The first time through, there are range possible values.
n = rnd.nextInt(range);
} else {
// There are only range-1 possible values given that the
// last is excluded.
n = rnd.nextInt(range - 1);
// Work around last.
if (n >= last) { ++n; }
}
last = n;
return n;
}
public void remove() { throw new UnsupportedOperationException(); }
}
回答by Peter Lawrey
You can do something like
你可以做类似的事情
int[] values = new int[360];
values[0] = random.nextInt(n+1);
for(int i = 0; i < values.length; i++) {
values[i] = random.nextInt(n);
if (values[i-1] == values[i]) values[i] = n;
}
回答by OldCurmudgeon
You can even be super-simple:
你甚至可以超级简单:
public class NonRepeatingRandom extends Random {
private int last = -1;
@Override
public int nextInt(int i) {
int next = super.nextInt(i);
while ( next == last) {
next = super.nextInt(i);
}
return last = next;
}
}