我建议使用
Random.nextBoolean()
也就是说,Math.random() 0.5,因为你也用过。这是我的机器上的行为:
$ cat myProgram.java
public class myProgram{
public static boolean getRandomBoolean() {
return Math.random() < 0.5;
//I tried another approaches here, still the same result
}
public static void main(String[] args) {
System.out.println(getRandomBoolean());
}
}
$ javac myProgram.java
$ java myProgram ; java myProgram; java myProgram; java myProgram
true
false
false
true
不用说,每次都不能保证获得不同的值。在你的情况,但是,我怀疑
A)你不使用你认为你的代码,(如编辑错误的文件)
B)你没有在测试时编译你的不同尝试,或者
C)你正在使用一些非标准破碎的实现。