DnD_5e.dice package

Module contents

class DnD_5e.dice.Dice(**kwargs)[source]

Bases: object

This class represents a die

get_dice_tuple() Tuple[int, int][source]
Returns:

self._dice_tuple

Return type:

tuple of positive integers (dice num, dice type)

get_dice_num() int[source]
Returns:

dice num (the first part of the dice tuple)

Return type:

positive integer

get_dice_type() int[source]
Returns:

dice type (the second part of the dice tuple)

Return type:

positive integer

get_modifier() int[source]
Returns:

modifier

Return type:

int

get_adv() int[source]
Returns:

adv

Return type:

int

get_critable() bool[source]
Returns:

True if a critical roll makes sense, False otherwise

Return type:

bool

get_max_value() int[source]
Returns:

the maximum number that could result from a roll

Return type:

int

get_min_value() int[source]
Returns:

the minimum number that could result from a roll

Return type:

int

get_average_value() float[source]

Get the average value of the dice

Note

this assumes a fair die (i.e., each number the die can roll, or outcome, is equally likely) if self._adv is 0 (i.e., if the dice does not have advantage or disadvantage).

If self._adv is not 0, there is a small error margin (less than 10^-16 for a d20) due to floating point arithmetic

Formula and explanation found at https://anydice.com/articles/dice-and-averages/

Returns:

the average number that would result from a roll

Return type:

float

get_freqdist() FreqDist[source]
Returns:

the probability distribution for the rolls of this dice

Return type:

FreqDist

get_freq(sample: int) int[source]

Get the number of possible outcomes (roll combinations) that give a desired result :param sample: a number that could be rolled on this dice :type sample: int :return: the number of possible outcomes (roll combinations) that give a result of sample

get_prob(sample: int, kind: str = 'eq') float[source]

Get the probability of rolling a given number on this dice.

Parameters:
  • sample – a number that could be rolled on this dice

  • kind – eq (equal), lt (less than), le (less than or equal to), gt, or ge.

Returns:

the probability of rolling sample

Return type:

float

create_probdist()[source]

Create the dict that stores the (numerators for) probability of getting a certain result from the dice

set_modifier(modifier: int)[source]

Set _modifier to the given value

Parameters:

modifier (int) – the new modifier

Returns:

None

Raise:

ValueError if modifier is invalid

shift_modifier(modifier: int)[source]

Add modifier to _modifier

Parameters:

modifier (int) – the amount to shift by

Returns:

None

Raise:

ValueError if modifier is invalid

set_adv(adv: int)[source]

Set _adv to the given value

Parameters:

adv (int) – the new advantage

Returns:

None

shift_adv(adv: int)[source]

Add adv to _adv

Parameters:

adv (int) – the amount to shift by

Returns:

roll_dice(modifier: int = 0, adv: int = 0, crit: int = 0, crit_multiplier: int = 2) Tuple[int, int][source]

Roll dice (applying advantage as specified), add the modifier (but don’t go below 0), and return a result that includes the total number and an integer that indicates whether or not the roll was a crit

Parameters:
  • modifier (int) – the modifier to add after all dice have been rolled

  • adv (one of these integers: -1, 0, 1) – indicates whether this roll has advantage

  • crit (one of these integers: -1, 0, 1) – indicates whether to roll more dice (e.g., because a crit was rolled)

  • crit_multiplier (positive integer) – the multiplier for dice num if the hit is a crit

Returns:

a tuple that contains the total roll value as well as an indicator of whether the roll was a crit or not

Return type:

TYPE_ROLL_RESULT

class DnD_5e.dice.NullDice(**kwargs)[source]

Bases: Dice

For when you need a dice, but you don’t actually care about the results

class DnD_5e.dice.DamageDice(**kwargs)[source]

Bases: Dice

This class represents a damage die (includes a regular die and a damage type)

get_damage_type() str[source]
Returns:

damage type

Return type:

str

has_damage_type(damage_type: str) bool[source]
Parameters:

damage_type (str) – the damage type searched for

Returns:

True if self has the damage type specified

Return type:

bool

roll_dice(modifier: int = 0, adv: int = 0, crit: int = 0, crit_multiplier: int = 2) Tuple[source]

Roll dice (applying advantage as specified), add the modifier (but don’t go below 0), and return a namedtuple that contains the roll number and damage type

Parameters:
  • modifier (int) – the modifier to add after all dice have been rolled

  • adv (one of these integers: -1, 0, 1) – indicates whether this roll has advantage

  • crit (one of these integers: -1, 0, 1) – indicates whether to roll more dice

  • crit_multiplier (positive integer) – the multiplier for dice num if the hit is a crit

Returns:

a tuple that contains the total roll value as well as an indicator of whether the roll was a crit or not

Return type:

namedtuple that contains fields “roll_number” and “damage_type”

class DnD_5e.dice.NullDamageDice(**kwargs)[source]

Bases: NullDice, DamageDice

For when you need DamageDice but don’t care about the result (e.g., in an Attack that doesn’t deal damage)

class DnD_5e.dice.DiceBag(**kwargs)[source]

Bases: Dice

Container class that holds multiple Dice

get_dice_list() list[source]
Returns:

dice list

Return type:

list of Dice

get_max_value() int[source]
Returns:

the maximum number that could result from rolls of all dice combined

Return type:

int

get_min_value() int[source]
Returns:

the minimum number that could result from rolls of all dice combined

Return type:

int

get_average_value() float[source]
Returns:

the average number that would result from rolls of all dice combined

Return type:

float

create_probdist()[source]

Create the dict that stores the (numerators for) probability of getting a certain result from the dice :return:

add_dice(die)[source]

Append the given die to _dice_list

Parameters:

die – the die to add

Returns:

None

Raise:

ValueError if input is invalid

set_modifier(modifier: int)[source]

Set modifier for all Dice in the bag to the specified modifier

Parameters:

modifier (int) – modifier

Returns:

None

shift_modifier(modifier: int)[source]

Add modifier to each die’s modifier

Parameters:

modifier (int) – the amount to shift by

Returns:

None

Raise:

ValueError if modifier is invalid

set_adv(adv: int)[source]

Set adv for all Dice in the bag to the specified value

Parameters:

adv (int) – adv

Returns:

None

shift_adv(adv: int)[source]

Add adv to each die’s adv

Parameters:

adv (int) – the amount to shift by

Returns:

None

Raise:

ValueError if adv is invalid

roll_dice(modifier: int = 0, adv: int = 0, crit: int = 0, crit_multiplier: int = 2) Tuple[source]

Roll dice (applying advantage as specified), add the modifier (but don’t go below 0), and return the result

Parameters:
  • modifier (int) – the modifier to add after all dice have been rolled

  • adv (one of these integers: -1, 0, 1) – indicates whether this roll has advantage

  • crit (one of these integers: -1, 0, 1) – indicates whether to roll more dice (applies to each individual die)

  • crit_multiplier (positive integer) – the multiplier for dice num if the hit is a crit (applies to each individual die)

Returns:

a tuple that contains the total roll value as well as an indicator of whether the roll was a crit or not

Return type:

tuple (total number, list of all numbers rolled)

get_dice_tuple()[source]
Raise:

NotImplementedError

get_dice_num()[source]
Raise:

NotImplementedError

get_dice_type()[source]
Raise:

NotImplementedError

get_modifier()[source]
Raise:

NotImplementedError

get_adv()[source]
Raise:

NotImplementedError

get_critable()[source]
Raise:

NotImplementedError

class DnD_5e.dice.DamageDiceBag(**kwargs)[source]

Bases: DiceBag, DamageDice

A class for a DiceBag of DamageDice

get_damage_type_list() list[source]
Returns:

damage types of all dice in the bag

Return type:

list

get_damage_type_set() set[source]
Returns:

damage types of all dice in the bag

Return type:

set

has_damage_type(damage_type: str)[source]
Parameters:

damage_type (str) – the damage type searched for

Returns:

True if self has the damage type specified

Return type:

bool

add_dice(die)[source]

Append the given die to self._dice_list

Parameters:

die – the die to add

Returns:

None

Raise:

ValueError if input is invalid

roll_dice(modifier: int = 0, adv: int = 0, crit: int = 0, crit_multiplier: int = 2) Tuple[source]

Roll dice (applying advantage as specified), add the modifier (but don’t go below 0), and return the result

Parameters:
  • modifier (int) – the modifier to add after all dice have been rolled

  • adv (one of these integers: -1, 0, 1) – indicates whether this roll has advantage

  • crit (one of these integers: -1, 0, 1) – indicates whether to roll more dice (applies to each individual die)

  • crit_multiplier (positive integer) – the multiplier for dice num if the hit is a crit (applies to each individual die)

Returns:

a tuple that contains the total roll value as well as an indicator of whether the roll was a crit or not

Return type:

tuple (total number, namedtuple with roll number and damage type)

get_damage_type()[source]
Returns:

damage type

Return type:

str