next up previous contents
Up: Sample Package Named Craps Previous: Sample Mathematica Package

Using the Sample Mathematica Package Named Craps

In[1]:= $ContextPath     (* Just illustrating Contexts *)

Out[1]= {Global`, System`}

In[2]:= <<craps.m  (* read in package named Craps stored in craps.m *)

In[3]:= In[1]    (* check to see that $ContextPath has changed *)

Out[3]= {Craps`, Global`, System`}

In[4]:= toss    (* verify that toss function is hidden and not Global*)

Out[4]= toss

In[5]:= Outcome    (* simulate one trial *)

Out[5]= {1, 3}     (* 1 says the player won on trial 3 *)

In[6]:= Simulation[100] (* 100 simulations. See package for definition *)

Out[6]= {100, 0.38, 3.76, 0.05} 
In[7]:= ?DisplaySimulation  (* get info on function via usage statement *)

DisplaySimulation[seed,list] initializes the random number generator with the
   integer seed and displays a table of outcomes of Simulation[n] where
   n=list[[i]] for i=1 to Length[list].

In[7]:= n = 10^Range[4]    (* iterations list *)

Out[7]= {10, 100, 1000, 10000}

In[8]:= DisplaySimulation[113,n]    (* this takes a little while *)

Out[8]//MatrixForm= 
 
    # simulations   p_hat=P(pass)   E(length)       sigma of p_hat
     
    10              0.5             2.3             0.158113883
     
    100             0.41            3.26            0.05
     
    1000            0.486           3.379           0.0158113883
     
   10000           0.4932          3.3988          0.005

In[9]:= N[{244/495,557/165}]    (* actual probabilities {P,E} *)

Out[9]= {0.492929, 3.37576}

In[10]:= Quit    (* terminate session *)