蒙特卡洛骰子掷骰和硬币模拟

时间:2018-12-18 09:59:48

标签: java random montecarlo dice

在我最近的一个实验室中,我们被要求在Java中进行蒙特卡洛模拟,这个问题的措辞很奇怪,但是从我的收集中我们需要模拟掷骰子和挖掘比特币。基本上,每200滚动一个比特币就被开采一次,但是我们只想知道在开采一个比特币时滚动六次它所花费的次数,然后我们需要获得一个6滚动的平均数。一个基本的解决方案已经解决了,但它不正确,而比特币部分使我感到困惑,有人知道我可以如何改进我的代码。

r.fillna(0, downcast='infer')

demographic_y   A   B  C
demographic_x           
A               0  52  0
B              52   0  0
C               0   0  4

我声明了一个我没有使用过的times变量,因为我只是想弄清楚放置在哪里,请记住我对编程还是比较陌生,甚至我的示威者都说这是一个非常问题给我们。 谢谢

1 个答案:

答案 0 :(得分:0)

这个问题确实有点模糊。 我不明白您所说的“平均六次滚动”是什么意思,如果您能解释一下,我会改写代码...

从您的解释中我可以理解,我做到了:

import java.util.Random;

public class MonteCarloDiceRollBitCoin {

    public static void main(String args[]) {
    Random rand = new Random();
    int rolled = 0;
    int bitcoinMined = 0;
    int bitcoinMinedAtSix = 0;
    int sixCounter=0;
    for (int i = 0; i < 1000000; i++) { // MonteCarlo simulation 1000000 times
        int dice = rand.nextInt(6) + 1; // 6 is the maximum and the 1 is our minimum, like simulating a dice roll
        if (dice == 6) {
            rolled++; // increment the rolled variable if dice returns a 6
        }
        if ((i+1) % 200 == 0) {
            bitcoinMined++; // increment counter divides by 200 : bitcoin has been mined
            if(sixCounter==0) {
                sixCounter=rolled; // this will remember the amount of sixRoled to mine the FIRST bitcoin.
            }
            if (dice == 6) {
                bitcoinMinedAtSix++; // and we also have a dice=6
            }
        }
    }
    System.out.println("Six was rolled " + rolled + " times.");
    System.out.println("Six was rolled " + sixCounter + " times before the first bitcoin was mined.");
    System.out.println(bitcoinMined + " bitcoin(s) were mined.");
    System.out.println(bitcoinMinedAtSix + " bitcoin(s) at Dice=6 were mined.");
    }
}