This page describes two ways of calculation the sum of two dice rolls.

Simulation

Imports

import cats.implicits._

import spire.math._
import spire.algebra._

import axle._
import axle.stats._
import axle.game.Dice._

Simulate 10k rolls of two dice

val d6a = die(6)
// d6a: axle.stats.Distribution0[Int,spire.math.Rational] = ConditionalProbabilityTable0(Map(5 -> 1/6, 1 -> 1/6, 6 -> 1/6, 2 -> 1/6, 3 -> 1/6, 4 -> 1/6),d6)

val d6b = die(6)
// d6b: axle.stats.Distribution0[Int,spire.math.Rational] = ConditionalProbabilityTable0(Map(5 -> 1/6, 1 -> 1/6, 6 -> 1/6, 2 -> 1/6, 3 -> 1/6, 4 -> 1/6),d6)

import spire.implicits.IntAlgebra
// import spire.implicits.IntAlgebra

val histogram =
  (0 until 10000).
  map(i => d6a.observe + d6b.observe).
  tally
// histogram: Map[Int,Int] = Map(5 -> 1142, 10 -> 818, 6 -> 1390, 9 -> 1115, 2 -> 273, 12 -> 267, 7 -> 1671, 3 -> 571, 11 -> 547, 8 -> 1360, 4 -> 846)

Define visualization

import axle.visualize.Color.blue
import axle.visualize._
val chart = BarChart[Int, Int, Map[Int, Int], String](
  () => histogram,
  colorOf = _ => Color.blue,
  xAxis = Some(0),
  title = Some("d6 + d6"),
  labelAngle = Some(0d *: angleDouble.degree),
  drawKey = false)
// chart: axle.visualize.BarChart[Int,Int,Map[Int,Int],String] = BarChart(<function0>,false,700,600,50,0.8,20,50,80,Some(d6 + d6),None,Courier New,12,Palatino,20,Some(0),None,None,Some(UnittedQuantity(0.0,UnitOfMeasurement(degree,°,Some(http://en.wikipedia.org/wiki/Degree_(angle))))),<function1>,<function1>,<function1>)

Create SVG

import axle.web._
// import axle.web._

svg(chart, "d6plusd6.svg")

Observed d6 + d6

Distribution Monad

The distribution of two rolls combined can be produced with a for comprehension and charted directly.

Imports (Note: documentation resets interpreter here)

import spire.math._
import spire.algebra._

import cats.implicits._

import axle._
import axle.stats._
import axle.game.Dice.die

Create probability distribution of the addition of two 6-sided die:

val distribution = for {
  a <- die(6)
  b <- die(6)
} yield a + b
// distribution: axle.stats.Distribution0[Int,spire.math.Rational] = ConditionalProbabilityTable0(Map(5 -> 1/9, 10 -> 1/12, 6 -> 5/36, 9 -> 1/9, 2 -> 1/36, 12 -> 1/36, 7 -> 1/6, 3 -> 1/18, 11 -> 1/18, 8 -> 5/36, 4 -> 1/12),unnamed)

Define visualization

import axle.visualize._
import axle.visualize.Color.blue
import spire.implicits.IntAlgebra
val chart = BarChart[Int, Rational, Distribution0[Int, Rational], String](
  () => distribution,
  colorOf = _ => Color.blue,
  xAxis = Some(Rational(0)),
  title = Some("d6 + d6"),
  labelAngle = Some(0d *: angleDouble.degree),
  drawKey = false)
// chart: axle.visualize.BarChart[Int,spire.math.Rational,axle.stats.Distribution0[Int,spire.math.Rational],String] = BarChart(<function0>,false,700,600,50,0.8,20,50,80,Some(d6 + d6),None,Courier New,12,Palatino,20,Some(0),None,None,Some(UnittedQuantity(0.0,UnitOfMeasurement(degree,°,Some(http://en.wikipedia.org/wiki/Degree_(angle))))),<function1>,<function1>,<function1>)

Create SVG

import axle.web._
// import axle.web._

svg(chart, "distributionMonad.svg")

Monadic d6 + d6