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) ┤▇▇▇▇▇▇▇▇ 455 [ 5.0, 6.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 896 [ 6.0, 7.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1603 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1842 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1916 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1607 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 898 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇ 473 [12.0, 13.0) ┤▇▇▇ 159 └ ┘ 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) ┤ 5 [ 4.0, 5.0) ┤▇ 30 [ 5.0, 6.0) ┤▇▇ 68 [ 6.0, 7.0) ┤▇▇▇▇ 164 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇ 290 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 489 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 714 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 988 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1079 [12.0, 13.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1276 [13.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1360 [14.0, 15.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1217 [15.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1041 [16.0, 17.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 696 [17.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇ 430 [18.0, 19.0) ┤▇▇▇▇ 153 └ ┘ 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) ┤ 26 [ 2.0, 4.0) ┤▇▇▇▇ 215 [ 4.0, 6.0) ┤▇▇▇▇▇▇▇ 388 [ 6.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 617 [ 8.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 840 [10.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1036 [12.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1149 [14.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1399 [16.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1640 [18.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1774 [20.0, 22.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 916 └ ┘ 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