10  Analysis of roll of dice

In this chapter let’s analyze the roll of pair of dice, and see what odds of betting will make you win. For that let’s define a variable named faces, that holds a range of numbers from 1 to 6.

(def faces (range 1 7))
faces
(1 2 3 4 5 6)

Now let’s generate combinations of die throws. So in the code below x and y are variables that hold the numbers from 1 to 6. The for function generates a list of vectors containing all possible combinations of x and y.

(def combinations
  (for [x faces y faces] [x y]))
combinations
([1 1]
 [1 2]
 [1 3]
 [1 4]
 [1 5]
 [1 6]
 [2 1]
 [2 2]
 [2 3]
 [2 4]
 [2 5]
 [2 6]
 [3 1]
 [3 2]
 [3 3]
 [3 4]
 [3 5]
 [3 6]
 [4 1]
 [4 2]
 [4 3]
 [4 4]
 [4 5]
 [4 6]
 [5 1]
 [5 2]
 [5 3]
 [5 4]
 [5 5]
 [5 6]
 [6 1]
 [6 2]
 [6 3]
 [6 4]
 [6 5]
 [6 6])

Now let’s sum up those combinations:

(map #(apply + %) combinations)
(2
 3
 4
 5
 6
 7
 3
 4
 5
 6
 7
 8
 4
 5
 6
 7
 8
 9
 5
 6
 7
 8
 9
 10
 6
 7
 8
 9
 10
 11
 7
 8
 9
 10
 11
 12)

Let’s use frequencies to see which number comes up most often when summed up:

(def frequency-of-sums (frequencies (map #(apply + %) combinations)))
frequency-of-sums
{7 6, 4 3, 6 5, 3 2, 12 1, 2 1, 11 2, 9 4, 5 4, 10 3, 8 5}

Now let’s sort frequency-of-sums by its values:

(sort-by val > frequency-of-sums)
([7 6] [6 5] [8 5] [9 4] [5 4] [4 3] [10 3] [3 2] [11 2] [12 1] [2 1])

Looks like when a pair of dice is thrown, number 7 comes up most often, followed by 6, 8, and 9. So better bet on number 7 when you throw a pair of dice.

10.1 Plotting

TBD

source: notebooks/stats_with_clojure/dice.clj