好的,我已经将这个stackoverflow问题实现到我的代码中:Return True or False Randomly
但是,我遇到了奇怪的行为:我需要同时运行十个实例,每个实例每次运行只返回一次true或false。而令人惊讶的是,无论我做什么,每次都只得到false
有什么方法可以改进这种方法,以便我至少有大约50%的机会获得true
吗?
为了更加易于理解:我将我的应用程序构建为JAR文件,然后通过批处理命令运行。
java -jar my-program.jar
pause
程序内容 - 为了尽可能简单:
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());
}
}
如果我打开10个命令行并运行它,每次都会得到结果为false
...
Random r = new Random(); r.nextBoolean();
? - Thomas Jungblut