Craps
and is available in the file
/home/banach/common/mathica/craps.m local users to examine or
copy. Unix users on the Sun network can copy it into their
mathica directory, by entering the following in a cmdtool window:
cp /home/banach/common/mathica/craps.m /home/banach/username/mathica/
comments:
(* The following package is a rather
sophisticated version of simulations used in studying the probability
of winning and the number of tosses of a pair of dice in the game of
"craps". On the first toss of the pair of dice the player wins on a
sum of 7 or 11 and loses on a sum of 2,3, or 12. Any other sum is the
player's point and the player wins if his point appears in subsequent
tosses of the pair of dice before a 7 occurs.
After installing the package (say located in craps.m) by executing
<<path/craps.m, three functions are available: Outcome,
Simulation[n], and DisplaySimulation[seed,list].
A function or procedure, such as Outcome below, which is to be
available outside the package, i.e. a global variable, must have a
corresponding usage definition following the BeginPackage. This usage
statement will be printed as help when ?Outcome is executed. The
function definition occurs after the Begin["`private`"] and is printed
as help by executing ??Outcome. A function such as toss below, with no
usage statement, is local to the package. *)
BeginPackage["Craps`"] (* adds `Craps to $ContextPath *)
Outcome::usage =
"Outcome is a function without arguments which returns a two element list
{x,n} with x=0 if the player lost (no pass) or x=1 if the player won (pass)
and n=(# of tosses before an outcome was determined) thus simulating one
play at a craps table."
Simulation::usage =
"Simulation[n_] returns a four element list {n,p_hat,L,sigma} where
n=(# of plays at the game of craps), p_hat=(# of passes)/n to estimate
P(pass) = the probability of a win, L=(# of tosses)/n to estimate
E(length) = the expected number of tosses per play, and sigma=1/Sqrt[4n]
is the upper bound for the standard deviation of p_hat."
DisplaySimulation::usage =
"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]."
Begin["`private`"]
(* toss returns integer from 2 to 12 simulating one throw of a pair of dice. *)
toss:= Random[Integer,{1,6}] + Random[Integer,{1,6}]
Outcome:=
Module[{rolls=1,point=toss,trial,pass=1,nopass=0},
trial:=Switch[rolls++;toss,7,{nopass,rolls},point,{pass,rolls},_,trial];
Which[MemberQ[{7,11},point],{pass,rolls},
MemberQ[{2,3,12},point],{nopass,rolls},
True,trial]]
Simulation[num_Integer]:=
Module[{result={0,0}},
Do[result+=Outcome,{num}];
Prepend[N[Append[result/num,1/Sqrt[4 num]],10],num]]
DisplaySimulation[seed_Integer,iterations_List]:=
CompoundExpression[
SeedRandom[seed],
MatrixForm[Join[{{"# simulations","p_hat=P(pass)","E(length)",
"sigma of p_hat"}},
Array[Simulation[iterations[[#]]]&,Length[iterations]]]
] ]
End[] (* private context *)
EndPackage[] (* Craps *)