Module:Dice: Difference between revisions

1,402 bytes removed ,  20:03, 31 July 2019
m
get up to date with repo version
(add error checking)
m (get up to date with repo version)
Line 2: Line 2:
     Dice.lua
     Dice.lua


     The core repository for this Lua module can be found at:
     The core repository and documentation for this Lua module can be found at:
     https://bitbucket.org/HeladoDeBrownie/dice/
     https://bitbucket.org/HeladoDeBrownie/dice/


Line 17: Line 17:
--# Interface
--# Interface


--[[
    Get a dice distribution from a dice string, which is in the form of a list
    of terms separated by + and -. Each term is either:
    - a dice roll expression of the form XdY, where X and Y are integers;
    - or a constant integer X, which is interpreted as Xd1.
    The terms are added and subtracted, as specified by + and - respectively,
    to form a compound dice distribution.
    For example, Dice.fromDiceString'5d3+1d2-1' gets the distribution that's
    represented by rolling 5 3-sided dice, rolling a 2-sided die, and
    subtracting 1 from their sum.
--]]
function Dice.fromDiceString(diceString)
function Dice.fromDiceString(diceString)
     local diceTable = {}
     local diceTable = {}
Line 63: Line 49:
end
end


-- Dice.fromString is an alias for Dice.fromDiceString.
Dice.fromString = Dice.fromDiceString
Dice.fromString = Dice.fromDiceString


--[[
    Get a dice distribution from a range string, which is of the form X-Y,
    where X and Y are nonnegative integers.
    This has the limitation that it doesn't accept negative numbers. To specify
    a distribution involving negative numbers, use the more general
    Dice.fromDiceString instead.
    For example, Dice.fromRangeString'4-8' gets the distribution that gives
    integers 4 through 8 with equal probability, equivalent to the distribution
    given by the dice string '1d5+3'.
--]]
function Dice.fromRangeString(rangeString)
function Dice.fromRangeString(rangeString)
     local minimum, maximum = rangeString:gsub('%s', ''):match'^(%d+)-(%d+)$'
     local minimum, maximum = rangeString:gsub('%s', ''):match'^(%d+)-(%d+)$'
Line 98: Line 71:
end
end


-- Get the minimum possible value of the distribution.
function Dice:minimum()
function Dice:minimum()
     local sum = 0
     local sum = 0
Line 113: Line 85:
end
end


--[[
    Get the mean value of the distribution, defined as the mean of all the
    possible values the distribution can take on weighted by their probability.
--]]
function Dice:mean()
function Dice:mean()
     local sum = 0
     local sum = 0
Line 127: Line 95:
end
end


-- Get the maximum possible value of the distribution.
function Dice:maximum()
function Dice:maximum()
     local sum = 0
     local sum = 0