How to resolve the algorithm 100 prisoners step by step in the Mathematica/Wolfram Language programming language
How to resolve the algorithm 100 prisoners step by step in the Mathematica/Wolfram Language programming language
Table of Contents
Problem Statement
Show and compare the computed probabilities of success for the two strategies, here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 100 prisoners step by step in the Mathematica/Wolfram Language programming language
This Wolfram code simulates two strategies for playing a game where you choose a number from 1 to 100, and the goal is to avoid having your number drawn from a sample of 50 random numbers. The first strategy (PlayRandom
) chooses a random number and keeps drawing random numbers until your number is drawn, while the second strategy (PlayOptimal
) chooses a random number and then sequentially checks the numbers in the sample until your number is found.
Here's a detailed breakdown of the code:
-
ClearAll[PlayRandom, PlayOptimal]
: This line clears any previous definitions of thePlayRandom
andPlayOptimal
functions. -
PlayRandom[n_] :=
: This line defines thePlayRandom
function, which takes one argument,n
, representing the number of times to play the game. -
Inside the
PlayRandom
function:Module[{pardoned = 0, sampler, indrawer, found, reveal}, ...]: This line creates a local scope for the variables
pardoned,
sampler,
indrawer,
found, and
reveal`.sampler = indrawer = Range[100];
: This line initializes bothsampler
andindrawer
to a list of numbers from 1 to 100.sampler
will be used to randomly select numbers, whileindrawer
will hold the remaining undrawn numbers.Do[ ..., {n}]:
This line starts a loop that runsn
times, simulatingn
games.- Inside the loop:
indrawer //= RandomSample;
: This line randomly shuffles theindrawer
list.found = 0;
: This line initializes thefound
variable to 0, indicating that the player's number has not yet been found.Do[ ..., {p, 100}]:
This line starts a nested loop that iterates through all numbers from 1 to 100.- Inside the nested loop:
reveal = RandomSample[sampler, 50];
: This line randomly selects 50 numbers from thesampler
list and assigns them to thereveal
variable.If[MemberQ[indrawer[[reveal]], p], ...]:
This conditional statement checks if the player's numberp
is among the 50 numbers inreveal
. If it is, thefound
variable is incremented by 1.
If[found == 100, pardoned++];
: This conditional statement checks if the player's number was found in all 100 iterations of the nested loop. If it was, thepardoned
variable is incremented by 1, indicating that the player successfully avoided having their number drawn.
- Finally, the function returns the fraction of games where the player's number was not drawn (
N[pardoned/n]
).
-
PlayOptimal[n_] :=
: This line defines thePlayOptimal
function, which also takes one argument,n
, representing the number of times to play the game. -
Inside the
PlayOptimal
function:Module[{pardoned = 0, indrawer, reveal, found, card}, ...]:
This line creates a local scope for the variablespardoned
,indrawer
,reveal
,found
, andcard
.indrawer = Range[100];
: This line initializesindrawer
to a list of numbers from 1 to 100.Do[ ..., {n}]:
This line starts a loop that runsn
times, simulatingn
games.- Inside the loop:
indrawer //= RandomSample;
: This line randomly shuffles theindrawer
list.Do[ ..., {p, 100}]:
This line starts a nested loop that iterates through all numbers from 1 to 100.- Inside the nested loop:
reveal = p;
: This line setsreveal
to the current numberp
, which represents the player's number.found = False;
: This line initializesfound
toFalse
, indicating that the player's number has not yet been found.Do[ ..., {g, 50}]:
This line starts a loop that iterates 50 times.- Inside the innermost loop:
card = indrawer[[reveal]];
: This line assigns the value at thereveal
index ofindrawer
to thecard
variable.If[card == p, ...]:
This conditional statement checks if thecard
value is equal to the player's numberp
. If they are equal, it means the player's number has been found, sofound
is set toTrue
, and theBreak[]
statement is used to exit the innermost loop.reveal = card;
: If thecard
value is not equal to the player's number,reveal
is updated to the value ofcard
.
- After the innermost loop completes, if
found
is stillFalse
, it means the player's number was not found in the 50 iterations, and the outer loop is exited using theBreak[]
statement.
If[found, pardoned++];
: This conditional statement checks if the player's number was found. If it was not found,pardoned
is incremented by 1, indicating that the player successfully avoided having their number drawn.
- Finally, the function returns the fraction of games where the player's number was not drawn (
N[pardoned/n]
).
-
PlayRandom[1000]
: This line calls thePlayRandom
function 1000 times and prints the fraction of games where the player's number was not drawn. -
PlayOptimal[10000]
: This line calls thePlayOptimal
function 10000 times and prints the fraction of games where the player's number was not drawn.
Source code in the wolfram programming language
ClearAll[PlayRandom, PlayOptimal]
PlayRandom[n_] :=
Module[{pardoned = 0, sampler, indrawer, found, reveal},
sampler = indrawer = Range[100];
Do[
indrawer //= RandomSample;
found = 0;
Do[
reveal = RandomSample[sampler, 50];
If[MemberQ[indrawer[[reveal]], p],
found++;
]
,
{p, 100}
];
If[found == 100, pardoned++];
,
{n}
];
N[pardoned/n]
]
PlayOptimal[n_] :=
Module[{pardoned = 0, indrawer, reveal, found, card},
indrawer = Range[100];
Do[
indrawer //= RandomSample;
Do[
reveal = p;
found = False;
Do[
card = indrawer[[reveal]];
If[card == p,
found = True;
Break[];
];
reveal = card;
,
{g, 50}
];
If[! found, Break[]];
,
{p, 100}
];
If[found, pardoned++];
,
{n}
];
N[pardoned/n]
];
PlayRandom[1000]
PlayOptimal[10000]
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the ALGOL 60 programming language
You may also check:How to resolve the algorithm Goldbach's comet step by step in the Julia programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the Raku programming language
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Y combinator step by step in the F# programming language