【发布时间】:2012-10-31 06:58:01
【问题描述】:
可能重复:
True random generation in Java
Java random always returns the same number when I set the seed?
我在我的一个程序中运行这段代码。
public String[] gen_phase_zero() {
Random generator = new Random();
int r = generator.nextInt(2);
if (r == 1) {
String big = "A";
String small = "a";
return new String[] { big, small };
} else {
String big = "B";
String small = "b";
return new String[] { big, small };
}
}
如果我运行几次,我的输出是这样的。
啊 氨基酸 氨基酸 氨基酸 BB 氨基酸 氨基酸 氨基酸 咩
并不总是这样。但它几乎从来没有接近 50/50
更新:
我不期待五十五十,但似乎如果先选择“Aa”,那么接下来会多选 3 次左右,但如果先选择 Bb,则会再选 3 次也是。
【问题讨论】:
-
当然你不能依赖
Random类来生成具有50-50概率的随机数。 -
见this问题。