~~Dices.jl~~ Moved to DiceRolls.jl
A package for dealing with dices in Julia.
Description
This package defines dices and some operations with them. Dice is the basic unit of the package. It can be created with a simple call:
using Dices
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 Dices:
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 dices.
using UnicodePlots
v = [roll(3d4) for _ = 1:10000]
UnicodePlots.histogram(v)
┌ ┐ [ 3.0, 4.0) ┤▇▇▇ 148 [ 4.0, 5.0) ┤▇▇▇▇▇▇▇▇ 433 [ 5.0, 6.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 925 [ 6.0, 7.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1555 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1896 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1849 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1586 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 965 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇ 495 [12.0, 13.0) ┤▇▇▇ 148 └ ┘ 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) ┤ 9 [ 4.0, 5.0) ┤▇ 29 [ 5.0, 6.0) ┤▇▇ 91 [ 6.0, 7.0) ┤▇▇▇▇ 144 [ 7.0, 8.0) ┤▇▇▇▇▇▇▇▇ 295 [ 8.0, 9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇ 484 [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 737 [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 928 [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1130 [12.0, 13.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1257 [13.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1310 [14.0, 15.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1223 [15.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1034 [16.0, 17.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 731 [17.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 446 [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) ┤ 20 [ 2.0, 4.0) ┤▇▇▇▇ 225 [ 4.0, 6.0) ┤▇▇▇▇▇▇▇ 396 [ 6.0, 8.0) ┤▇▇▇▇▇▇▇▇▇▇▇ 616 [ 8.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 813 [10.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 923 [12.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1202 [14.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1378 [16.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1680 [18.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1822 [20.0, 22.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 925 └ ┘ Frequency
Histogram
Lastly, we define a function histogram
that computes all combinations and the histogram of results.
results, frequency = Dices.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 = Dices.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