Aces Up

Steven J Miller: sjm1@williams.edu

acesup[numiter_] := Module[{},
(* computes the probability last four cards are in different suits *)
theory = (52/52) (39/51)(26/50)(13/49) * 1.0; (* theoretical answer *)
success = 0; (* prob have four final cards in four suits *)
deck = {}; (* initializes deck to empty, adds then 13 1's, 13 10's, 13 100's, 13 1000 *)
(* last four cards are different suits if sum to 1111, else have a repeat *)
For[j = 0, j ≤ 3, j++,
For[i = 1, i ≤ 13, i++, AppendTo[deck,10^j]];
]; (* end of j loop *)
For[n = 1, n ≤ numiter, n++, (* this is the main loop *)
{
(* prints out every 10% so can see how you far we are *)
(* printing slows down the program, but only significantly if numiter is small *)
If[Mod[n,numiter/10] == 0, Print["We have done ", n*100.0/numiter, "%."]];
temp = RandomSample[deck,4]; (* chooses the last four cards *)
value  = Sum[temp[[k]],{k,1,4}]; (* calculates sum of last four cards *)
If[value == 1111, success = success + 1]; (* if sum is 1111, increase success by 1 *)
}]; (* end of the n loop *)
Print["Theory: ", theory*100, "%."]; (* print results *)
Print["Observed: ", success * 100.0 / numiter, "%."]; 
]
Timing[acesup[1000000]]

Timing[acesup[10000000]]

Timing[acesup[100000000]]