Chuck-A-Luck is a casino game played with three dice with six different ways to bet. We will focus on the number bet, where you bet on a number from 1 to 6, and for our example the payout will be $1 if your number comes up once,
$2 if it comes up twice, and $5 if it comes up six times. How much would you be willing to pay to play this game?
We begin by simulating rolls of three dice. We set the variables n.trials, n.sides, and n.rolls for the number of simulations, sides on the dice, and how many dice to roll, respectively. The functionsample, inside thereplicatefunc- tion, will select three (rolls) random integers from 1 up to die with replace- ment. This will be repeated trials times and replicate returns a 3×trials matrix assigned simulation. Note if we aren’t sure if an object is a data frame or a matrix we can use is.data.frame and is.matrix to check. Within the square brackets of simulation the first location is blank, which is read as return all rows, while the second value is 1:10, which is read as return columns 1 through 10.
R Code
> n.trials=100000
> n.side=6
> n.rolls=3
> simulation=replicate(trials, sample(n.side,n.rolls,replace=TRUE))
> simulation[,1:10]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 4 2 1 2 3 3 3 3 3
[2,] 4 3 3 1 1 3 4 4 3 1
[3,] 4 6 3 2 1 5 5 5 6 3
Since the values 1 through 6 are equally likely, without loss of generality, we can count the number of times the die showed 1 in each column of simulation.
Within thecolSums function, which sums the columns of a matrix, we have the logical statement simulation==1. Hence,colSumswill only sum the cells that satisfy the statement. We set the output to chuck and list out the first 10 values with chuck[1:10]. To check our work we can compare the output with that of simulation[,1:10].
R Code
> chuck=colSums(simulation==1)
> chuck[1:10]
Chuck-A-Luck 173
[1] 1 0 0 2 2 0 0 0 0 1
To calculate expected winnings we set the variables m1, m2, m3 to the payouts for rolling a one 1 to 3 times. Wetabulatethe vector chuck for values up to 3 so that wins is a vector of length three for the number of times one was rolled once, twice, and three times. The value of return is the total won in playing the game trials times. Our expected winnings is return/trials. Paying more than $0.51 to play is unwise.
R Code
> m1=1
> m2=2
> m3=5
> wins=tabulate(chuck,3)
> return=wins[1]*m1+wins[2]*m2+wins[3]*m3
> return/trials [1] 0.5121
Out of curiosity we ask the question, how will our winnings change if the number of sides of the die increases? We answer this question with a graph.
We will take the previous work and create a function for repeated use with different sizes of dice.
We set game to be a function of the number of trials n.trials, the number of sides on the dice n.sides, the number of dice to roll n.dice, and the payout for 1, 2 or 3 ones on the dice in pay.1, pay.2, pay.3. The code within the curly brackets for function is essentially the same as above with some changes in variable names. The one key is that we need to addreturn(expected.money) before ending the function. We then set max.die=20 for the maximum number of sides on the dice. The sapply function applies what is in the first input into the function of the second input and returns a vector of the same size.
Note that gameis already a function and it would seem that all we would need is game(1000,x,3,1,2,5)in the second input. But any variable can be used in the second input so it isn’t clear that we wish to apply the numbers 1:max.die to x. Hence the use of function(x). Ingamewe have 100000 trials, 3 dice and 1, 2, and 5 as our payouts. The result of sapply is set to dist.
We list the first five elements of dist. We should note that sapply and for loops are very similar and we could have used dist=numeric(max.die); for(i in 1:max.die)dist[i]=game(100000,i,3,m1,m2,m3) instead. In using a for loop here we have to create the vector dist first whereas we don’t need to do this when usingsapply.
R Code
> game=function(n.trials,n.sides,n.dice,pay.1,pay.2, pay.3){
+ simulation=replicate(n.trials,sample(n.sides,n.dice, replace=TRUE))
+ chuck=colSums(simulation==1) + wins=tabulate(chuck,3)
+ money=wins[1]*pay.1+wins[2]*pay.2+wins[3]*pay.3 + expected.money=money/n.trials
+ return(expected.money)}
> max.die=20
> dist=sapply(1:max.die,function(x) {game(100000,x,3,1,2,5)})
> dist[1:5]
[1] 5.00000 1.74779 1.07258 0.78285 0.61214
We plot our results withplot. Thexvalues are 1:max.die, and theyvalues are dist. The point character is set with pch=16, a solid dot, and the axes are labeled with xlab and ylab. Notice that if there is only 1 side on each die we always get three ones and so our payout is always $5. Moving to dice with two numbers cuts our expected value to under $2.
R Code
> plot(1:max.die,dist,pch=16,xlab=
"Number of Sides on Dice",ylab="Payout in $")
The Buffon Needle Problem 175
●
●
●
●
●
●
● ● ● ● ● ● ● ● ● ● ● ● ● ●
5 10 15 20
012345
Number of Sides on the Dice
Payout in $