## 15 June 2014

### Three Little Pigs

The other day I read a blog post on "Ripples, Very Small Waves in R"
http://aschinchon.wordpress.com/2014/06/08/the-three-little-pigs/
It is about a game of dice called "The Three Little Pigs" where only
one die is used. The rules are:

"Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player decides to hold. If the player rolls a 1, they score nothing and it becomes the next player’s turn. If the player rolls any other number, it is added to their turn total and the player’s turn continues. If a player chooses to hold, their turn total is added to their score, and it becomes the next player’s turn. The first player who reach at least 100 points is the winner."

I showed this game to my eight year old grandson and he was delighted. He calls the game "Cochinito" (little piggy) because in this game you are penalized by chance if you get too greedy. He is playing it with anyone and everyone he can talk into it. He kept talking about good luck and bad luck so much that I decided to teach him a little bit about probability. I showed him that with one die each number has an equal chance to land face up and since there are six numbers on the die the probability of throwing any particular number is 1/6 or in other words about 16.7 percent. I made some simulations and graphs in "R" programming with the help of "R Workshop for Beginners" (Barry Rowlingson,Lancaster University, UK http://www.maths.lancs.ac.uk/~rowlings/Teaching/Stafford2013-Sept/dice.html

The simulations let you make multiple rolls of the dice and let you see the distrbution of the multiple roll results. Here are the results for four sets of 100 roll trials with a single die:

1  2  3  4  5  6
18 18 15 15 22 12

1  2  3  4  5  6
15 17 15 19 13 21

1  2  3  4  5  6
19 18 14 18 20 11

1  2  3  4  5  6
19 16 19 18 14 14

Here is the result of a 10,000 roll trial:
1    2    3    4    5    6
1664 1639 1695 1651 1680 1671

You can see that as the number of multiple rolls increases the average gets closer to 16.7.

Here is the result order for the roll of one set of 100 rolls with a single die:
6 5 4 5 6 3 5 5 4 4 1 1 3 4 5 6 4 4 4 4 6 5 1 4 1 3 1 3 3 2 6 4 3 2 1 5 3 3 1 3 2 5 1 2 6 6 6 3 2 3 1 3 2 6 5 1 2 5 6 4 1 6 2 5 4 4 4 4 2 1 5 4 5 6 6 5 5 6 5 1 4 3 1 6 1 5 3 6 1 3 5 6 2 6 4 2 2 1 2 3

Each 100 rolls of a single die will result in a new random number. You can see that you never know when the number "1" will jump up and grab you or leave you alone for awhile.

This is the R code that I used:

table(as.integer(runif(10000, 1, 7)))  # method with one die

table(sample(1:6, 10000, replace = TRUE))  # Alternate  with one die

# Wrapped in a function and plotted - one die
die = function(n) {
return(sample(1:6, n, replace = TRUE))
}
table(die(10000))
plot(table(die(10000)))

# Wrapped in a function and plotted - two dice
die = function(n) {
return(sample(1:6, n, replace = TRUE))
}
table(die(10000))
plot(table(die(10000) + die(10000)))

# A dice function to throw one die 100 times and return the result.
die = function(n) {
return(sample(1:6, n, replace = TRUE))
}
dice = function(T = 100, N = 1) {
m = matrix(die(N * T), N, T)
return(colSums(m))
}
dice()

# A function to throw two dice 100 times and return the sum.
die = function(n) {
return(sample(1:6, n, replace = TRUE))
}
dice = function(T = 100, N = 2) {
m = matrix(die(N * T), N, T)
return(colSums(m))
}
dice()

This video by Eric Cai is very helpful in explaining the probability of rolling dice: