我如何总结通过循环获得的概率结果? (Java)
How can I summarize the probability results, that I got with a loop? (Java)
我想知道有没有办法总结我通过一个循环得到的概率结果?所以我可以知道有多少成功的命中,给定 x 次尝试。现在我只得到 1 或 0 个语句流(1 表示成功命中,0 表示失败),不太实用。它看起来像这样:
public class doGry {
public static void main(String[] args ) {
for (int i = 0; i < 50; i++) {
// chance to hit (h) = 35% + (ma - md)
// 8% < h < 90%
double ma = 20;
double md = 10;
double probability;
System.out.println("probability of success " + (probability = 35 + (ma - md)));
double probab2 = probability / 100;
double r = Math.random();
int roll;
if (r <= probab2) roll = 1;
else roll = 0;
System.out.println(roll);
}
}
}
您所要做的就是添加一个变量来跟踪发生了多少次成功掷骰,然后像这样使用双精度数除以掷骰总数
public class doGry
{
public static void main(String[] args )
{
int total = 0;
for (int i = 0; i < 50; i++)
{
// chance to hit (h) = 35% + (ma - md)
// 8% < h < 90%
double ma = 20.0;
double md = 10.0;
double probability;
System.out.println("probability of success " + (probability = 35 + (ma - md)));
double probab2 = probability / 100;
double r = Math.random();
int roll;
if (r <= probab2) {roll = 1; total++;}
else roll = 0;
System.out.println(roll);
}
System.out.println("total: " + total/50.0);
}
}
注意在最后的 System.out.println 语句中,总数除以 50.0(而不是 50),因为 .0 表示您想要双除法而不是整数除法,后者会丢弃任何余数小数值。
很少有变量可以在 for 循环之外初始化,因为您不会修改它的值,所以不需要它一次又一次地初始化它。
使用变量 count
来跟踪成功的命中。对于每个成功的命中,将其值增加 1。
public class doGry {
public static void main(String[] args ) {
double ma = 20;
double md = 10;
double probability = 35 + (ma - md);
double probab2 = probability / 100;
int count = 0;
for (int i = 0; i < 50; i++) {
System.out.println("probability of success " + probability);
double r = Math.random();
if (r <= probab2)
{
System.out.println("1");
count++;
}
else
System.out.println("0");
}
System.out.println("Succesful hits " + count);
}
}
我想知道有没有办法总结我通过一个循环得到的概率结果?所以我可以知道有多少成功的命中,给定 x 次尝试。现在我只得到 1 或 0 个语句流(1 表示成功命中,0 表示失败),不太实用。它看起来像这样:
public class doGry {
public static void main(String[] args ) {
for (int i = 0; i < 50; i++) {
// chance to hit (h) = 35% + (ma - md)
// 8% < h < 90%
double ma = 20;
double md = 10;
double probability;
System.out.println("probability of success " + (probability = 35 + (ma - md)));
double probab2 = probability / 100;
double r = Math.random();
int roll;
if (r <= probab2) roll = 1;
else roll = 0;
System.out.println(roll);
}
}
}
您所要做的就是添加一个变量来跟踪发生了多少次成功掷骰,然后像这样使用双精度数除以掷骰总数
public class doGry
{
public static void main(String[] args )
{
int total = 0;
for (int i = 0; i < 50; i++)
{
// chance to hit (h) = 35% + (ma - md)
// 8% < h < 90%
double ma = 20.0;
double md = 10.0;
double probability;
System.out.println("probability of success " + (probability = 35 + (ma - md)));
double probab2 = probability / 100;
double r = Math.random();
int roll;
if (r <= probab2) {roll = 1; total++;}
else roll = 0;
System.out.println(roll);
}
System.out.println("total: " + total/50.0);
}
}
注意在最后的 System.out.println 语句中,总数除以 50.0(而不是 50),因为 .0 表示您想要双除法而不是整数除法,后者会丢弃任何余数小数值。
很少有变量可以在 for 循环之外初始化,因为您不会修改它的值,所以不需要它一次又一次地初始化它。
使用变量 count
来跟踪成功的命中。对于每个成功的命中,将其值增加 1。
public class doGry {
public static void main(String[] args ) {
double ma = 20;
double md = 10;
double probability = 35 + (ma - md);
double probab2 = probability / 100;
int count = 0;
for (int i = 0; i < 50; i++) {
System.out.println("probability of success " + probability);
double r = Math.random();
if (r <= probab2)
{
System.out.println("1");
count++;
}
else
System.out.println("0");
}
System.out.println("Succesful hits " + count);
}
}