在掷骰子游戏中用10,000次模拟计算获胜概率(获胜/(胜利+损失))..这是作业的一部分

时间:2016-12-04 06:29:26

标签: java simulation probability

在掷骰子游戏中使用10,000次模拟计算获胜概率(wins/ (wins + losses))。这是掷骰子游戏的方法:

public class CrapsGame {

    public static void main(String[] args) {
            int dice1 = (int)(Math.random()* 6) + 1;
            int dice2 = (int)(Math.random()* 6) + 1;
            int roll = dice1 + dice2;
            System.out.println();
            System.out.print("You rolled "+roll+". ");
            if(roll == 2 || roll == 3 || roll == 12){
                    System.out.println("You Lose !");
            }else if(roll == 7 || roll == 11){
                    System.out.println("You Win !");
            }else{
                    System.out.println("Point is "+roll+"\n");
                    dice1 = (int)(Math.random()* 6) + 1;
                    dice2 = (int)(Math.random()* 6) + 1;
                    int roll2 = dice1 + dice2;
                    System.out.print("You rolled "+roll2+". ");
                    while(roll2 != 7){
                            if(roll == roll2){
                                    System.out.println("You Win !");
                                    break;
                            }else{
                                    System.out.println("Point is "+roll+"\n");
                            }
                            dice1 = (int)(Math.random()* 6) + 1;
                            dice2 = (int)(Math.random()* 6) + 1;
                            roll2 = dice1 + dice2;
                            System.out.print("You rolled "+roll2+". ");
                    }
                    if(roll2 == 7){
                            System.out.println("You Lose !");
                    }                       
            }
    }
}

我不认为这应该是困难的,我只需要代码运行10,000次模拟,然后计算概率。谢谢:))

是否可以让某人插入此

的工作版本

在逻辑之外放置一段时间或for循环并创建2个计数器(timesWinning,timesLosing)。在现有代码内增加每个。循环运行10.000次后,根据需要进行数学计算:wins /(wins + loss)

谢谢你这是作业的一部分

2 个答案:

答案 0 :(得分:1)

你应该在最后修改他的代码:

 System.out.println("Probability of winning: " +  ((double)timesWon/(timesWon + timesLost)));

我的结果来自于我自己:

你玩过:10000.0,获胜:5078,获胜概率:0.5078

你参加过:1.0E8,获胜:50707214,获胜概率:0.50707214

答案 1 :(得分:0)

您的代码已经具备了所有必需的逻辑。要多次重复它,有不同的方法。它们被称为loops。如果您在Google上进行搜索,您会看到forwhiledo/while和其他一些人(代码中已有while)。

对于你的问题,基本的是重复10,000次相同的事情。

public class CrapsGame {

    public static void main(String[] args) {

        // Create some variables to control the times it won and times it lost
        int timesWon = 0;
        int timesLost = 0;


        // Here we're saying it's gonna repeat 10000 times, incrementing 1 by 1
        for(int i=0; i<10000; i++) {
            // Here you will insert all the existing logic from your program

            // In the existing code, increment the variables declared according

            if(roll == 2 || roll == 3 || roll == 12){
                System.out.println("You Lose !");
                timesLost++;
            }else if(roll == 7 || roll == 11){
                System.out.println("You Win !");
                timesWon++;
            }else{
                // Where you find it won, insert `timesWon++;`
                // Where you find it lost, insert `timesLost++;`
            }
        }
        // Here it exits the for loop (after 10000 times)
        // Here's where you can calculate

        System.out.println("Probability of winning: " + (timesWon/(timesWon + timesLost)));
    }
}

这应足以获得所需的结果。

希望它有所帮助。