Java 掷5个骰子,看3个是否相同
完整问题:模拟重复试验,以估计掷五个骰子并获得至少三个相同数字的概率 我知道如何通过反复试验来检查概率。我只是不知道如何判断五分之三的骰子是否相同。需要注意的重要一点是:我们还没有在课堂上学习数组,所以我不能使用它。我们已经学习了对象、决策,目前正在学习循环。如果您能帮助检查三个是否相同,我们将不胜感激。以下是我现在拥有的:Java 掷5个骰子,看3个是否相同,java,loops,random,dice,Java,Loops,Random,Dice,完整问题:模拟重复试验,以估计掷五个骰子并获得至少三个相同数字的概率 我知道如何通过反复试验来检查概率。我只是不知道如何判断五分之三的骰子是否相同。需要注意的重要一点是:我们还没有在课堂上学习数组,所以我不能使用它。我们已经学习了对象、决策,目前正在学习循环。如果您能帮助检查三个是否相同,我们将不胜感激。以下是我现在拥有的: Random rand = new Random(); final int TRIALS = 100_000; int same = 0;//check
Random rand = new Random();
final int TRIALS = 100_000;
int same = 0;//check if they are the same??
for(int i=0; i<TRIALS; i++){
int a = rand.nextInt(6)+1;
int b = rand.nextInt(6)+1;
int c = rand.nextInt(6)+1;
int d = rand.nextInt(6)+1;
int e = rand.nextInt(6)+1;
}
Random rand=new Random();
最终int试验=100_000;
int-same=0//检查它们是否相同??
对于(int i=0;i我想这应该适用于您:
Random rand = new Random();
final int TRIALS = 100;
int same = 0;//check if they are the same??
int aux = 0;
for(int i=0; i<TRIALS; i++){
aux = 0;
int a = rand.nextInt(6)+1;
int b = rand.nextInt(6)+1;
int c = rand.nextInt(6)+1;
int d = rand.nextInt(6)+1;
int e = rand.nextInt(6)+1;
if(a == b || a == c || a == d || a == e)
aux++;
if(b == c || b == d || b == e)
aux++;
if(c == d || c == e)
aux++;
if(d == e)
aux++;
if(aux > 2)
same++;
}
System.out.println("The posibilities are: " + same + "/" + TRIALS);
}
Random rand=new Random();
最终int试验=100;
int same=0;//检查它们是否相同??
int aux=0;
对于(int i=0;i 2)
相同的++;
}
System.out.println(“可能性为:“+相同+”/“+试验);
}
尽量不要使用任何你现在知道的东西
你也可以试试这个,同样的,但你会有确切的问题:
final int TRIALS = 100;
int same = 0;//check if they are the same??
int aux = 0;
for(int i=0; i<TRIALS; i++){
aux = 0;
int a = rand.nextInt(6)+1;
int b = rand.nextInt(6)+1;
int c = rand.nextInt(6)+1;
int d = rand.nextInt(6)+1;
int e = rand.nextInt(6)+1;
if(a == b || a == c || a == d || a == e)
aux++;
if(b == c || b == d || b == e)
aux++;
if(c == d || c == e)
aux++;
if(d == e)
aux++;
if(aux > 2)
same++;
}
double prob = same/TRIALS;
System.out.println("The posibilities are: " + prob);
}
final int TRIALS=100;
int same=0;//检查它们是否相同??
int aux=0;
对于(int i=0;i 2)
相同的++;
}
双探针=相同/试验;
System.out.println(“可能性为:“+prob”);
}
所以我在这里做的只是模拟1000次,每次模拟滚动5个骰子,并通过递增表示结果的整数变量(即1、2等)记录每个结果,最后,如果任何整数大于2(即5次中出现3次以上),然后我将增加相同的值,然后你可以将其除以1000,得到概率
Random rand = new Random();
final int TRIALS = 1000;
int same = 0;//check if they are the same??
for(int i = 0; i < TRIALS; i++){
int ones = 0;
int twos = 0;
int threes = 0;
int fours = 0;
int fives = 0;
int sixes = 0;
for (int j = 0; j < 5; j++) {
int curr = rand.nextInt(6) + 1;
if (curr == 1) {
ones++;
} else if (curr == 2) {
twos++;
} else if (curr == 3) {
threes++;
} else if (curr == 4) {
fours++;
} else if (curr == 5) {
fives++;
} else if (curr == 6) {
sixes++;
}
}
if (ones > 2 || twos >= 2 || threes >= 2 || fours > 2 || fives > 2 || sixes > 2) {
same++;
}
}
double probability = (double)same/TRIALS;
Random rand=new Random();
最终int试验=1000;
int same=0;//检查它们是否相同??
对于(int i=0;i2 | 2>=2 | 3>=2 | 4>2 | 5>2 | 6>2){
相同的++;
}
}
双重概率=(双重)相同/试验;
场景:
掷骰子五个骰子特定时间,并根据结果计算至少3个数字等于(4和5)的估计概率
流程图:
我创建了一个流程图,以便您能够理解正在计算的内容
代码:
import java.util.Random;
公共类掷骰子
{
公共静态void main(字符串[]args)
{
Random rand=新的Random();
int等于0;
int不等于0;
最终int试验=567;
整数骰子=0;
int-one=0;
int 2=0;
int三=0;
int-four=0;
int五=0;
int-six=0;
对于(int i=0;i=3||
两个>=3||
三个>=3||
四个>=3||
五个>=3||
六个>=3)
{
System.out.println(“耶!一个数字至少重复3次”);
System.out.println(“[“+1+”、“+2+”、“+3+”、“+4+”、“+5+”、“+6+”)”);
三等于++;
}
其他的
{
System.out.println(“没有数字重复3次”);
System.out.println(“[“+1+”、“+2+”、“+3+”、“+4+”、“+5+”、“+6+”)”);
不等于++;
}
1=0;
二=0;
三=0;
四=0;
五=0;
六=0;
}
System.out.println(“至少三个等于计数:+threeEquals”);
System.out.println(“小于三个等于计数:+notThreeEquals”);
System.out.println(“试验:+试验”);
System.out.println(“估计概率:”+((双)三等/试验)*100+“%”;
}
}
输出:
没有数字重复3次
。。。
[0,0,1,2,1,1]
没有数字重复3次
[1,0,1,0,1,2]
耶!一个数字至少重复3次
[0,1,0,0,3,1]
耶!一个数字至少重复3次
[0,0,2,3,0,0]
至少三个等于计数:119
少于三个等于计数:448
审判:567
估计概率:20.98765432098765%
额外:
你可以不用掷骰子来计算概率。你需要问“它们都没有发生的概率是多少?”然后从1中减去(因为“没有发生”的补充事件是“至少发生一次”)
我检查了5个掷骰子内的所有可能性,并计算了其中哪一个有3个或更多重复的数字,结果是:
3的概率等于:
3的概率不等于:
总之,最终公式为:
例子:
根据上述公式:
正如您所见,每次尝试5掷骰子,您至少获得三个相等数字的概率都会大大增加。您想要这个概率还是检查是否确实是3个相同的数字,并返回真/假?点击@Thrasher评论。这个概率可以在不实际掷骰子的情况下找到,因此我等待com的回复是的。
import java.util.Random;
public class RollDices
{
public static void main ( String [ ] args )
{
Random rand = new Random();
int threeEquals=0;
int notThreeEquals=0;
final int TRIALS = 567;
int dice = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
for(int i=0; i<TRIALS; i++)
{
for(int j = 0; j < 5; j++)
{
dice = rand.nextInt(6)+1;
if(dice == 1) one++;
if(dice == 2) two++;
if(dice == 3) three++;
if(dice == 4) four++;
if(dice == 5) five++;
if(dice == 6) six++;
}
if( one >= 3 ||
two >= 3 ||
three >= 3 ||
four >= 3 ||
five >= 3 ||
six >= 3)
{
System.out.println ( "Yay! one number is repeated at least 3 times" );
System.out.println ( "[" + one + "," + two +","+ three +","+ four +","+ five +","+ six +"]");
threeEquals++;
}
else
{
System.out.println ( "No number is repeated 3 times" );
System.out.println ( "[" + one + "," + two +","+ three +","+ four +","+ five +","+ six +"]");
notThreeEquals++;
}
one = 0;
two = 0;
three = 0;
four = 0;
five = 0;
six = 0;
}
System.out.println ( "At least three equals count: " + threeEquals );
System.out.println ( "Less than three equals count: " + notThreeEquals );
System.out.println ( "Trials: " + TRIALS );
System.out.println ( "Estimated probability: " + ((double)threeEquals/TRIALS)*100 + "%");
}
}