“Random” numbers and Dice Rolls

Mathematica will generate pseudo-random numbers of various kinds using the built-in function Random[ ].  Here is a summary.

?Random

For example, you can get a random integer between 1 and 6 to simulate the roll of a die using Random[Integer,{1,6}].
 Here is a table of 100 of them

dicerolls = Table[Random[Integer,{1,6}],{i,1,100}]

Suppose we want to test whether a given outcome, say 3, occurs about 1/6 of the time, as expected.   We need to count the 3’s in the list. This is easy with the built-in function Tally[ ].

Tally[dicerolls]
Sort[%]

To get the frequencies we need to divide the counts by the total number of rolls.  We can do this efficiently by transposing this matrix of values and dividing the second row by the total number of rolls.  This will give a nice table of observed frequencies. We use the function N[ ] to give decimal approximations

dicefrequencies = Transpose[Sort[Tally[dicerolls]]]
dicefrequencies[[2]] = N[dicefrequencies[[2]]/Length[dicerolls]]
dicefrequencies=Transpose[dicefrequencies]

Here is a bar chart of the observed frequencies.

BarChart[Transpose[dicefrequencies][[2]]]

It would be nice to play around with larger number of dice rolls.  We can write a little program to make a table of frequencies for n rolls of the die.

DiceFrequencies[n_] := Module[ {dicerolls, dicefreqencies,i},
dicerolls = Table[Random[Integer,{1,6}],{i,1,n}];
dicefrequencies = Transpose[Sort[Tally[dicerolls]]];
dicefrequencies[[2]] = N[dicefrequencies[[2]]/Length[dicerolls]];
dicefrequencies=Transpose[dicefrequencies]]

Now we can just call this function to get any number, n, of dice rolls.  For large values of n, the frequencies are close to 1/6.

dicefreqs = DiceFrequencies[10000]

Chevalier de Méré

To simulate the dice game of the Chevalier, we need to generate four dice rolls and see if a 6 occurs at least once in the list of four outcomes.  We can write a function representing a Random Variable whose value is 1 if there is at least one six (so the Chevalier wins) and 0 otherwise (so the Chevalier loses).

RollFourDice[] := Table[Random[Integer,{1,6}],{i,1,4}]
ChevalierWins[{d1_,d2_,d3_,d4_}] := If[ d1==6 || d2==6 || d3==6 || d4 ==6,1,0]

RollFourDice[]
ChevalierWins[%]

We can make a table of wins and losses for 100 repetitions of the game.  If you try this over and over, you will see that sometimes he wins and sometimes he loses.  So the game would be interesting.

Table[ChevalierWins[RollFourDice[]],{i,1,100}]
Plus@@%

Now we want to find the frequency of winning in n repetitions of the game.  Just make a big list of 1’s and 0’s representing his wins and losses. Then take the sum using the Plus@@ operator to find the number of wins and compute the frequency.  (Note: the funny @@ symbol feeds the entries of a given list to a function, rather than the list itself with the curly brackets.)

ChevalierFrequency[n_] := Module[{winlist,i},
winlist =  Table[ChevalierWins[RollFourDice[]],{i,1,n}];
N[ Plus@@winlist/Length[winlist] ]
]

In the long run, we expect to approach the theoretical probablity 0.5177 ...

ChevalierFrequency[100000]