Java Helper For Exercise 19

average nbr of rolls needed to resolve pass line bet


  /********************

  file    :  Craps.java
  created :  25 may 2005  
  purpose :  Estimate the expected value of the random variable X,
             where X is the number of rolls of the dice needed to
             resolve a shooter's pass line bet.
  Updates:
    01 june 2005  Revise intro comment at top of approx_mu ftn

 
  ********************/

  import java.io.*;

  class     Craps 
    {
       private static double      approx_mu ( int NN )
         {
            /****
                  We want to approximate the expected value of 
                  X = nbr of rolls of the dice  needed to resolve 
                  a shooter's pass line bet.

                  The answer is given by the limit as NN approaches
                  infinity of mu, where

                  mu  =  1/3  +  (1/18) times the sum,
                         k running from  from 2 thru NN, of 
                         k * [ (3/4)**(k-1)  +  (10/9)*(13/18)**(k-2)  +
                         (55/36)*(25/36)**(k-2) ]

            ****/

            double  mu ;
            double  p1, p2, p3 ;

            double  sum = 0.0 ;

            for ( int k=2; k<=NN; k++ )
               {
                   p1 =  Math.pow( (3.0/4), k-1 ) ;
                   p2 =  (10.0/9)  * Math.pow( (13.0/18), k-2 ) ;
                   p3 =  (55.0/36) * Math.pow( (25.0/36), k-2 ) ;

                   sum += k * ( p1 + p2 + p3 ) ;
               }

            mu = (1.0/3)  +  (1.0/18)*sum ;

            return mu ;

         } // approx_mu


       public static void      main ( String [] arguments )
         {
             int      nbr_iterations ;
             double   my_ans ;

             System.out.println( "" ) ;

             nbr_iterations = 10 ;   my_ans = approx_mu ( nbr_iterations ) ;

             System.out.println( "For " + nbr_iterations  + 
                                 " iterations, "          +
                                 "approx mean is "        +  my_ans ) ;
             System.out.println( "" ) ;

             nbr_iterations = 80 ;  my_ans = approx_mu ( nbr_iterations ) ;

             System.out.println( "For " + nbr_iterations  + 
                                 " iterations, "          +
                                 "approx mean is "        +  my_ans ) ;
             System.out.println( "" ) ;


             nbr_iterations = 1000 ;  my_ans = approx_mu ( nbr_iterations ) ;

             System.out.println( "For " + nbr_iterations  + 
                                 " iterations, "          +
                                 "approx mean is "        +  my_ans ) ;
             System.out.println( "" ) ;

             nbr_iterations = 10000 ;  my_ans = approx_mu ( nbr_iterations ) ;

             System.out.println( "For " + nbr_iterations  + 
                                 " iterations, "          +
                                 "approx mean is "        +  my_ans ) ;
             System.out.println( "" ) ;

             System.out.println( "Ba-Bye from Craps." ) ;
             System.out.println( "" ) ;

         } // main

    } // Craps class