Algs4-1.1.35模拟投骰子

1.1.35模拟投骰子。以下代码能够计算每两个骰子之和的准确概率分布:
public class Test
{
    public static void main(String[] args)
    {
        int SIDES=6;
        double[] dist=new double[2*SIDES+1];
        for (int i=1;i<=SIDES;i++)
            for (int j=1;j<=SIDES;j++)
                 dist[i+j]+=1.0;
       
        for (int k=2;k<=2*SIDES;k++)
            dist[k]/=36.0;
        
    }
 }
dist[i]的值就是两个骰子之和为i的概率。用实验模拟N次掷骰子,并在计算两个1到6之间的随机整数之和时记录每个值的出现频率以验证它们的概率。N要多大才能够保证你的经验数据和准确数据的吻合程度达到小数点后三位?
解:约10^8次
public class Test
{
    public static void main(String[] args)
    {
     
        int SIDES=6;
        //二维数组,第一维用来存两个骰子掷出的点数和,第二维的1元素存理论概率,第二维的2元素存已掷到的次数,第二维的3元素存实际概率
        double[][] dist=new double[2*SIDES+1][3];
        for (int i=1;i<=SIDES;i++)
            for (int j=1;j<=SIDES;j++)
                 dist[i+j][0]+=1.0;
       //
        for (int k=2;k<=2*SIDES;k++)
            dist[k][0]/=36.0;
       //
        int N=0;
        int dist1;
        int dist2;
       //isOK=true时表示实际概率满足精度要求
        boolean isOK=false;
       while (!isOK)
        {
         N++;
         dist1=StdRandom.uniform(1,SIDES+1);
         dist2=StdRandom.uniform(1,SIDES+1);
         dist[dist1+dist2][1]++;
         dist[dist1+dist2][2]=dist[dist1+dist2][1]/N;
         //
         isOK=true;
         //所有点数和的实际概率都满足精度时才视为满足精度
         for(int i=2;i<=2*SIDES;i++)
         {
             if (Math.abs(dist[i][0]-dist[i][2])>=0.0001)
             {
                 isOK=false;
                 break;
             }  
         }//end for
       }//end while
       StdOut.printf("Limt times is %d",N);
   }
}
图片

原文地址:https://www.cnblogs.com/longjin2018/p/9848732.html