~~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) ┤▇▇ 139                                    
   [ 4.0,  5.0) ┤▇▇▇▇▇▇▇▇ 464                              
   [ 5.0,  6.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 964                     
   [ 6.0,  7.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1585         
   [ 7.0,  8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1907   
   [ 8.0,  9.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1853    
   [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1519          
   [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 976                     
   [11.0, 12.0) ┤▇▇▇▇▇▇▇▇ 449                              
   [12.0, 13.0) ┤▇▇▇ 144                                   
                └                                        ┘ 
                                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) ┤▇ 28                                      
   [ 5.0,  6.0) ┤▇▇ 74                                     
   [ 6.0,  7.0) ┤▇▇▇▇ 158                                  
   [ 7.0,  8.0) ┤▇▇▇▇▇▇▇▇ 306                              
   [ 8.0,  9.0) ┤▇▇▇▇▇▇▇▇▇▇▇ 413                           
   [ 9.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 648                     
   [10.0, 11.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 909              
   [11.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1141       
   [12.0, 13.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1269    
   [13.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1307   
   [14.0, 15.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1298   
   [15.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1061         
   [16.0, 17.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 784                  
   [17.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇ 431                           
   [18.0, 19.0) ┤▇▇▇▇ 164                                  
                └                                        ┘ 
                                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) ┤▇ 31                                      
   [ 2.0,  4.0) ┤▇▇▇▇ 188                                  
   [ 4.0,  6.0) ┤▇▇▇▇▇▇▇▇ 416                              
   [ 6.0,  8.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇ 609                          
   [ 8.0, 10.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 813                      
   [10.0, 12.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1007                  
   [12.0, 14.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1171               
   [14.0, 16.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1389           
   [16.0, 18.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1602      
   [18.0, 20.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 1783   
   [20.0, 22.0) ┤▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 991                   
                └                                        ┘ 
                                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