Card Game and Marriage

Steven Miller October 2016

Card Game

acesversusfours[numdo_] := Module[{},
deck = {1,1,1,1,10,10,10,10}; (* inputs special cards *)
For[n = 1, n ≤ 44, n++, deck = AppendTo[deck,0]]; (* fills rest of deck *)
count = 0; (* will count how often end on card 29 *)
winsky = 0; winjohn = 0; (* set wins of each player to 0 *)
For[n = 1, n ≤ numdo, n++, (* plays game the number of times we wish *)
{ (* this is the beginning of the n for loop *)
shuffle = RandomSample[deck,52]; (* shuffles the deck randomly *)
If[shuffle[[29]] > 0, (* this is true if and only if 29th card is a 1 or 10 *)
{ (* start of if loop where game MAY end on 29th; must make sure didn't end earlier *)
sum = Sum[shuffle[[n]],{n,1,28}]; (* computes sum of first 28 cards *)
If[Mod[sum,10] < 4 && sum < 35, (* only these cases game ends on 29th *)
{ (* start of if loop for game ending on 29th card; earlier was just POSSIBLE end *)
newsum = sum + shuffle[[29]];
If[Mod[newsum,10] ≥ 4 || newsum ≥40,
{
count = count + 1; (* increase count by 1 as game ends here *)
(* next line increments winner by 1 *)
If[shuffle[[29]] == 1, winsky = winsky+1, winjohn = winjohn+1];
}]; (* end of if statement that ensures we will win at 29 *)
}]; (* end of games ending at card 29 *)

}];  (* end of if statement for shuffle[29] *)
}]; (* end of n for loop *)
(* next few lines print *)
Print["In ",numdo, " attempts we had ", count, "  or ", 100. count/numdo, "% finish at card 29."];
Print["Prob(skynet win) = ", 100. * winsky/count, "."];
Print["Prob(john   win) = ", 100. * winjohn/count, "."];
];

Timing[acesversusfours[3000000]]

Marriage/Secretary Problem

secretaryproblem[k_,numpeople_,numdo_] := Module[{},
(* numdo is the number of times we play, numpeople is number of people, k is where we stop looking to build intuition *)
success = 0;
people = {};
If[k == numpeople, Print["YOU ARE AN IDIOT."]];
For[p = 1, p ≤ numpeople, p++, people = AppendTo[people,p]];  (* makes list of people *)
For[n = 1, n ≤ numdo, n++,
{
(* the higher the score, the better the person *)
ordergroup = RandomSample[people,numpeople];
bestofk =  0;
For[j = 1, j ≤ k, j++, 
If[ordergroup[[j]] > bestofk, bestofk = ordergroup[[j]]]]; (* end of j loop *)
For[j = k+1, j ≤ numpeople, j++,
{
If[ordergroup[[j]] > bestofk,
{
If[ordergroup[[j]] == numpeople, success = success + 1];
j = numpeople + 100; (* break us out of the j loop *)
}]; (* end of If statement where found someone better than first k *)
}]; (* end of j loop *)
}]; (* end of n loop *)
Print["If k =approx= numpeople/e expect ", 100. / E];
Print["We won ", 100. success/numdo, "%."];
]; (* end of module *)

Timing[secretaryproblem[Floor[1000/E],1000,10000]]