API Reference
ndice - A dice rolling library for games.
- class ndice.Dice(*args: Any, **kwargs: Any)
A single term in a dice expression like 2d6 or -2.
Instances of
Diceare immutable.Diceinstances are also interned, meaning each unique instance is cached and reused instead of creating duplicates.- classmethod die(sides: int) Dice
Return a
Diceinstance that represents a single die.- Parameters:
sides (int) – The number of sides on the die. Must be >= 0.
>>> Dice.die(12) Dice(1, 12)
d()is an alias forDice.die().>>> d(8) Dice(1, 8)
- classmethod mod(value: int, op: Op = Op.PLUS) Dice
Return a
Diceinstance that represents a modifier.- Parameters:
value (int) – The magnitude of the modifier.
op (Op) – The operation of the modifier.
Mods are represented as
Diceinstances wherenumberis the constant value of the mod andsidesis 1.>>> Dice.mod(2) Dice(2, 1)
>>> Dice.mod(2, Op.MINUS) Dice(2, 1, Op.MINUS)
If
valueis negative andopisOp.PLUSorOp.MINUS, thenopand the sign ofvalueare inverted. IfopisOp.TIMESthenvaluemust be non-negative.>>> Dice.mod(-2) Dice(2, 1, Op.MINUS) >>> Dice.mod(-3, Op.MINUS) Dice(3, 1) >>> Dice.mod(-4, Op.PLUS) Dice(4, 1, Op.MINUS)
mod()is an alias forDice.mod().>>> mod(10, Op.TIMES) Dice(10, 1, Op.TIMES)
- classmethod n_dice(number: int, sides: int | Dice) Dice
Return a
Diceinstance that represents a number of dice.- Parameters:
number (int) – The number of dice. Must be >= 0.
sides (int | Dice) – The number of sides on each die. Must be >= 0.
>>> Dice.n_dice(2, 8) Dice(2, 8)
If the
sidesargument is aDiceinstance instead of anint, itssidesattribute is used for the returnedDiceinstance and its other attributes are ignored.>>> from ndice import d4 >>> Dice.n_dice(2, d4) Dice(2, 4)
nd()is an alias forDice.n_dice().>>> nd(4, 6) Dice(4, 6)
- property is_mod: bool
Is this dice term a modifier?
A modifier (mod) will always evaluate to a constant value. Mods like +1, -2 and x5 are represented as
Diceinstances wherenumberis the constant value of the mod andsidesis 1.Dicewithnumberorsidesequal to zero are also considered mods and always evaluate to zero.
- number: int
The number of dice to roll; must be zero or greater.
- sides: int
The number of sides of each die; must be zero or greater.
- class ndice.Op(*values)
Operations applied to terms in dice expressions.
An
Opis a binary operation applied to twointvalues. ThreeOpcases are defined:PLUS,MINUS, andTIMES.Opcases are sortable and ordered, withPLUSfirst andTIMESlast.>>> Op.PLUS < Op.MINUS < Op.TIMES True
Each
Opcase has two attributes:symbolandf.The
symbolattribute is astrcontaining a single character:'+','-'or'x'respectively. Note that the symbol forOp.TIMESis the letterxrather than the symbol*.The
fattribute contains a function that implements the binary operation.Opcases are also callable, executingfon the provided arguments.>>> Op.PLUS(1, 2) 3
- MINUS = ('-', <slot wrapper '__sub__' of 'int' objects>)
The subtraction operation
- PLUS = ('+', <slot wrapper '__add__' of 'int' objects>)
The addition operation
- TIMES = ('x', <slot wrapper '__mul__' of 'int' objects>)
The multiplication operation
- class ndice.SequenceRNG(*values: int)
A fake number generator that returns values from a given sequence.
SequenceRNGinstances are callable objects that match theRNGtype alias.>>> srng = SequenceRNG(2, 5, 1, 4) >>> [srng(6) for _ in range(3)] [2, 5, 1]
You can check if all values in the
SequenceRNGhave been consumed using theis_emptyproperty.>>> srng.is_empty False >>> srng(6) 4 >>> srng.is_empty True
Calling a
SequenceRNGinstance with no remaining values will raise an exception.- property is_empty: bool
Have all values in the sequence have been produced?
- ndice.AscendingRNG(initial_value: int) Callable[[int], int]
Create a fake number generator that returns ascending values.
The returned
RNGcallable produces an ascending sequence of values, starting withinitial_value.>>> arng = AscendingRNG(2) >>> [arng(6) for _ in range(3)] [2, 3, 4]
If the internal value is less than 1 or greater than the
sidesargument, theRNGwill apply the modulus functions so that values wrap to the required range of[1, sides].>>> arng = AscendingRNG(0) >>> [arng(6) for _ in range(3)] [6, 1, 2]
>>> arng = AscendingRNG(7) >>> [arng(6) for _ in range(3)] [1, 2, 3]
- Parameters:
initial_value (int)
- Returns:
a fake number generator that returns ascending values.
- ndice.FixedRNG(fixed_value: int) Callable[[int], int]
Create a fake number generator that returns a fixed value.
The returned
RNGcallable produces values based a fixed value.>>> always_2 = FixedRNG(2) >>> [always_2(6) for _ in range(3)] [2, 2, 2]
If the fixed value is less than 1, 1 is returned.
>>> always_1 = FixedRNG(0) >>> [always_1(6) for _ in range(3)] [1, 1, 1]
If the fixed value is greater than the
sidesargument,sidesis returned.>>> always_6 = FixedRNG(6) >>> always_6(6) 6 >>> always_6(4) 4
- Parameters:
fixed_value (int)
- Returns:
a fake number generator that returns a fixed value.
- ndice.PRNG(seed: int) Callable[[int], int]
Create a pseudo-random number generator.
The returned
RNGcallable produces a deterministic sequence of values based on the seed value. The returned number generator is useful when reproducibility is needed, such as for testing.>>> prng = PRNG(1122334455) >>> sides = 6 >>> [prng(sides) for _ in range(10)] [4, 4, 2, 6, 3, 1, 1, 1, 5, 3]
- Parameters:
seed (int) – The seed value that selects a the deterministic sequence.
- Returns:
a pseudo-random number generator.
- ndice.d(sides: int) Dice
Return a
Diceinstance that represents a single die.- Parameters:
sides (int) – The number of sides on the die. Must be >= 0.
>>> Dice.die(12) Dice(1, 12)
d()is an alias forDice.die().>>> d(8) Dice(1, 8)
- ndice.high(sides: int) int
A fake number generator that always returns the highest roll.
>>> [high(6) for _ in range(3)] [6, 6, 6]
- ndice.low(sides: int) int
A fake number generator that always returns the lowest roll.
>>> [low(6) for _ in range(3)] [1, 1, 1]
The lowest roll is always 1.
- ndice.max_roll(*dice_expression: Dice) int
The maximum total for a dice expression.
>>> from ndice import d6, plus, times >>> max_roll(d6, plus(2), times(10)) 80
This is equivalent to
roll(high, ...).- Parameters:
dice_expression (Dice) – A list of dice and modifiers.
- Returns:
The maximum total for the dice expression.
- ndice.mid(sides: int) int
A fake number generator that always returns the middle roll.
If the number of sides is odd, the middle value in [1, sides] is returned.
>>> [mid(3) for _ in range(3)] [2, 2, 2]
If the number of sides is even, the lower of the two middle values in [1, sides] is returned.
>>> [mid(6) for _ in range(3)] [3, 3, 3]
- ndice.min_roll(*dice_expression: Dice) int
The minimum total for a dice expression.
>>> from ndice import d6, plus, times >>> min_roll(d6, plus(2), times(10)) 30
This is equivalent to
roll(low, ...).- Parameters:
dice_expression (Dice) – A list of dice and modifiers.
- Returns:
The minimum total for the dice expression.
- ndice.minus(value: int | Dice) Dice
Make a subtractive dice term.
>>> minus(2) Dice(2, 1, Op.MINUS)
>>> from ndice import d6 >>> minus(d6) Dice(1, 6, Op.MINUS)
- Parameters:
value (int | Dice) – A
intmod value or existingDiceinstance.- Returns:
A new dice term with operation
Op.MINUS.
- ndice.mod(value: int, op: Op = Op.PLUS) Dice
Return a
Diceinstance that represents a modifier.- Parameters:
value (int) – The magnitude of the modifier.
op (Op) – The operation of the modifier.
Mods are represented as
Diceinstances wherenumberis the constant value of the mod andsidesis 1.>>> Dice.mod(2) Dice(2, 1)
>>> Dice.mod(2, Op.MINUS) Dice(2, 1, Op.MINUS)
If
valueis negative andopisOp.PLUSorOp.MINUS, thenopand the sign ofvalueare inverted. IfopisOp.TIMESthenvaluemust be non-negative.>>> Dice.mod(-2) Dice(2, 1, Op.MINUS) >>> Dice.mod(-3, Op.MINUS) Dice(3, 1) >>> Dice.mod(-4, Op.PLUS) Dice(4, 1, Op.MINUS)
mod()is an alias forDice.mod().>>> mod(10, Op.TIMES) Dice(10, 1, Op.TIMES)
- ndice.nd(number: int, sides: int | Dice) Dice
Return a
Diceinstance that represents a number of dice.- Parameters:
number (int) – The number of dice. Must be >= 0.
sides (int | Dice) – The number of sides on each die. Must be >= 0.
>>> Dice.n_dice(2, 8) Dice(2, 8)
If the
sidesargument is aDiceinstance instead of anint, itssidesattribute is used for the returnedDiceinstance and its other attributes are ignored.>>> from ndice import d4 >>> Dice.n_dice(2, d4) Dice(2, 4)
nd()is an alias forDice.n_dice().>>> nd(4, 6) Dice(4, 6)
- ndice.plus(value: int | Dice) Dice
Make an additive dice term.
>>> plus(1) Dice(1, 1)
>>> from ndice import d4 >>> plus(d4) Dice(1, 4)
- Parameters:
value (int | Dice) – A
intmod value or existingDiceinstance.- Returns:
A new dice term with operation
Op.PLUS.
- ndice.rng(sides: int) int
A truly random number generator.
>>> assert 1 <= rng(6) <= 6
Uses the global
Random.randrange()function.
- ndice.roll(rng: Callable[[int], int], *dice_expression: Dice) int
Roll a set of dice and apply modifiers.
An empty dice expression returns zero.
>>> from ndice import mid >>> roll(mid) 0
Terms in the dice expression are totalled up from left to right. (The
Dice.opattribute does not change the order of operations.)>>> from ndice import d6, minus, times >>> roll(mid, d6, minus(1), times(10)) 20
- Parameters:
rng (RNG) – A random number generator.
dice_expression (Dice) – A list of dice and modifiers.
- Returns:
The total rolled for the dice expression.
- ndice.roll_each_die(rng: Callable[[int], int], dice: Dice) list[int]
Roll the given dice or mod, returning each die value rolled.
>>> from ndice import mid, three_d6 >>> roll_each_die(mid, three_d6) [3, 3, 3]
If the
diceparameter is a modifier, a list containing the mod value is returned. Thedice.opattribute is not applied to the returned value.>>> from ndice import mod, Op, rng >>> minus_2 = mod(-2) >>> roll_each_die(rng, minus_2) [2]
- Parameters:
rng (RNG) – A random number generator.
dice (Dice) – A dice term or mod.
- Returns:
A list of the rolled die values.
- ndice.simplify_dice(*expression: Dice) list[Dice]
Join adjacent plus and minus mods together.
- Parameters:
expression (list[Dice]) – the dice expression to simplify
- Returns:
the simplfied dice expression
>>> from ndice import d6, minus, plus >>> simplify_dice(d6, plus(3), minus(1)) [Dice(1, 6), Dice(2, 1)]