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) ┤▇▇ 141                                    
   [ 4.0,  5.0) ┤▇▇▇▇▇▇▇▇ 473                              
   [ 5.0,  6.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 924                      
   [ 6.0,  7.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1569          
   [ 7.0,  8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1950   
   [ 8.0,  9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1880    
   [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1532          
   [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 929                      
   [11.0, 12.0) ┤▇▇▇▇▇▇▇▇ 438                              
   [12.0, 13.0) ┤▇▇▇ 164                                   
                └                                        ┘ 
                                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) ┤ 6                                        
   [ 4.0,  5.0) ┤▇ 31                                      
   [ 5.0,  6.0) ┤▇▇ 80                                     
   [ 6.0,  7.0) ┤▇▇▇▇ 163                                  
   [ 7.0,  8.0) ┤▇▇▇▇▇▇▇▇ 298                              
   [ 8.0,  9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 472                          
   [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 715                    
   [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 925              
   [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1176       
   [12.0, 13.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1255     
   [13.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1321   
   [14.0, 15.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1205      
   [15.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1009           
   [16.0, 17.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 775                  
   [17.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇ 402                            
   [18.0, 19.0) ┤▇▇▇▇ 167                                  
                └                                        ┘ 
                                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) ┤▇ 33                                      
   [ 2.0,  4.0) ┤▇▇▇▇ 219                                  
   [ 4.0,  6.0) ┤▇▇▇▇▇▇▇ 395                               
   [ 6.0,  8.0) ┤▇▇▇▇▇▇▇▇▇▇▇ 602                           
   [ 8.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 842                      
   [10.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 991                    
   [12.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1204               
   [14.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1366            
   [16.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1612       
   [18.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1826   
   [20.0, 22.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 910                     
                └                                        ┘ 
                                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