DiceRolls.jl
A package for dealing with dice in Julia.
Description
This package defines dice and some operations with them. Dice is the basic unit of the package. It can be created with a simple call:
using DiceRolls
Dice(6)
d₆
There are some existing dice already defined:
d4, d6, d8, d10, d12, d20
(d₄, d₆, d₈, d₁₀, d₁₂, d₂₀)
As expected you can perform some operations with dice:
3d6, 2d4 + 2, d4 + d6
(3d₆, 2d₄+2, 1d₄+1d₆)
And finally, you have one last "Dice" defined:
coin
1d₂-1
And naturally you can roll these dice.
using UnicodePlots
v = [roll(3d4) for _ = 1:10000]
UnicodePlots.histogram(v)
┌ ┐ [ 3.0, 4.0) ┤▇▇▇ 151 [ 4.0, 5.0) ┤▇▇▇▇▇▇▇▇▇ 469 [ 5.0, 6.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 889 [ 6.0, 7.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1604 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1869 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1857 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1606 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 922 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇ 478 [12.0, 13.0) ┤▇▇▇ 155 └ ┘ Frequency
Sum and Multiplication
You can sum and multiply dice:
d6 * d6 + d4 * d8
(d₆×d₆)+(d₄×d₈)
Drop and Keep
You can easily drop the lowest dice of a roll:
r = drop(4d6)
4d₆ drop lowest 1
v = [roll(r) for _ = 1:10000]
UnicodePlots.histogram(v)
┌ ┐ [ 3.0, 4.0) ┤ 4 [ 4.0, 5.0) ┤▇ 34 [ 5.0, 6.0) ┤▇▇ 79 [ 6.0, 7.0) ┤▇▇▇▇▇ 183 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇▇ 311 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 467 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 664 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 956 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1127 [12.0, 13.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1325 [13.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1324 [14.0, 15.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1232 [15.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1024 [16.0, 17.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 716 [17.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇ 402 [18.0, 19.0) ┤▇▇▇▇ 152 └ ┘ Frequency
Drop can also be used with argument kind=:highest
to drop the highest roll, and with n=<some number>
to drop more than one dice.
Keep works the same way except that it keeps the highest value by default. It accepts the same arguments.
r = keep(2d20)
2d₂₀ keep highest 1
v = [roll(r) for _ = 1:10000]
UnicodePlots.histogram(v)
┌ ┐ [ 0.0, 2.0) ┤ 19 [ 2.0, 4.0) ┤▇▇▇ 176 [ 4.0, 6.0) ┤▇▇▇▇▇▇▇▇ 407 [ 6.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇ 562 [ 8.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 802 [10.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 988 [12.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1203 [14.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1402 [16.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1618 [18.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1843 [20.0, 22.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 980 └ ┘ Frequency
Histogram
Lastly, we define a function histogram
that computes all combinations and the histogram of results.
results, frequency = DiceRolls.histogram(drop(3d4))
UnicodePlots.barplot(results, frequency)
┌ ┐ 2 ┤■■ 1 3 ┤■■■■■■■ 3 4 ┤■■■■■■■■■■■■■■■■ 7 5 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■ 12 6 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 16 7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 15 8 ┤■■■■■■■■■■■■■■■■■■■■■■■ 10 └ ┘
We can also pass normalize=true
to compute the probabilities instead.
results, frequency = DiceRolls.histogram(drop(3d4), normalize=true)
UnicodePlots.barplot(results, frequency)
┌ ┐ 2 ┤■■ 0.015625 3 ┤■■■■■■ 0.046875 4 ┤■■■■■■■■■■■■■■■ 0.109375 5 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.1875 6 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.25 7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.234375 8 ┤■■■■■■■■■■■■■■■■■■■■■ 0.15625 └ ┘
Statistics
You can compute some statistical information of a dice or roll with the function mean
, median
, std
and var
r = drop(3d4)
mean(r), median(r), std(r), var(r)
(5.9375, 6, 1.4670868242881878, 2.15234375)
Comparisons and Probabilities
Using comparison operators on a roll will return (a compact representation of) all rolls that satisfy that comparison. For instance,
r = drop(3d4)
collect(r > 7)
10-element Array{Array{Int64,1},1}: [4, 4, 1] [4, 4, 2] [4, 4, 3] [4, 1, 4] [4, 2, 4] [4, 3, 4] [1, 4, 4] [2, 4, 4] [3, 4, 4] [4, 4, 4]
collect(r == 7)
15-element Array{Array{Int64,1},1}: [4, 3, 1] [3, 4, 1] [4, 3, 2] [3, 4, 2] [4, 1, 3] [4, 2, 3] [4, 3, 3] [1, 4, 3] [2, 4, 3] [3, 4, 3] [3, 1, 4] [3, 2, 4] [1, 3, 4] [2, 3, 4] [3, 3, 4]
Using prob
one can compute the probability of that situation happening.
r = drop(4d6)
prob(r > 14)
0.23148148148148148