Skip to main content

Overview

multi_dice.py provides functions for rolling dice and calculating results for tabletop RPGs and other dice games. It handles standard dice rolls, advantage/disadvantage, modifiers, and selecting highest/lowest rolls.

Usage

To use the multi_dice module, first import it:

import multi_dice

To roll dice, call the roll function and pass the dice string:

result = multi_dice.roll("2d20+5")

To roll with advantage, add an 'a' before the dice string:

result = multi_dice.roll("a2d20+5")

To roll with disadvantage, add a 'd' before the dice string:

result = multi_dice.roll("d2d20+5")

Examples

  • "2d20" - Roll 2d20
  • "a2d20" - Roll advantage on 2d20
  • "d2d20" - Roll disadvantage on 2d20
  • "2d20+5" - Roll 2d20 and add 5
  • "4d6k3" - Roll 4d6 keep highest 3
  • "2d20l1-5" - Roll 2d20 keep lowest 1 and subtract 5
  • "2d8+1d6" - Roll 1d6 and add it to a roll of 2d8

Dice String Rules

Here are the rules for constructing a valid dice string:

  • "a" xor "d" (optional on beginning of string)
  • "A" xor "D" (optional on beginning of roll)
  • int
  • "d"
  • int
  • "k" xor "l" (optional)
    • int (requried if k xor l)
  • opcode (+, -, /, //, *, (**, ^, raise)) (optional)
    • int or another dice string (required if opcode)

OR

  • int

"a" = overall advantage - can only be used in the very beginning of the string
"d" = overall disadvantage - can only be used in the very beginning of the string
"A" = per roll advantage - can be used on any roll within the string
"D" = per roll disadvantage - can be used on any roll within the string
"k" = keep highest int rolls
"l" = keep lowest int rolls

Note:
When using multiple opcodes to do multiple dice, PEMDAS is only followed 0.0.7 onward. Eariler versions do not follow PEMDAS and could bug out in certain cases. Parenthesis are currently supported so you can get fancy with something like (1d12+1d6)**3 which does crash other dice rollers.

Using /,//, or any dis/advantage will not change the average calculation because it is just inserting the dice average itself into the same math equation. There is no real average formula being used other than per die average which only works on +,-,*,** etc.

Limits

The only checks on whether a roll is performed is if either the number of rolls or if the sides of the dice are over 1000, and if k and l are <= to the number of sides.

Functions

roll(dice)

Rolls dice according to the provided dice string and returns the result.

  • dice (str): The dice rolling string
  • Returns: (int) Result of the dice roll

advantage(*func,*args, *kwargs)

Calls a function twice and returns the higher result. Used for rolling with advantage.

  • func (function): The function to call
  • *args: Positional args to pass to the function
  • **kwargs: Keyword args to pass to the function
  • Returns: (int) The higher of the two function calls

disadvantage(*func,*args,*kwargs)

Calls a function twice and returns the lower result. Used for rolling with disadvantage.

  • func (function): The function to call
  • *args: Positional args to pass to the function
  • **kwargs: Keyword args to pass to the function
  • Returns: (int) The lower of the two function calls

Classes

RollDice

Class for rolling dice and calculating results. Handles parsing dice strings, rolling, modifiers, advantage/disadvantage, etc.

_init_(dice, crit=20)

  • dice (str): Dice rolling string

  • crit (int): Value that causes critical hit

.advantage(): Reroll with advantage, keeping higher value

.disadvantage(): Reroll with disadvantage, keeping lower value

.value: Result of the roll

.rolls: List of each individual dice roll

.average: Average possible roll result

.minimum: Minimum possible roll result

.maximum: Maximum possible roll result

.crit: True if a critical hit was rolled

.data: dict of all values

Class Examples

Regular usage

dice = multi_dice.RollDice("3d6")
print(dice.value)

Roll with dis/advantage:

dice = multi_dice.RollDice("a2d20")
print(dice.value)

dice2 = multi_dice.RollDice("d2d20")
print(dice2.value)

Release files for multi-dice 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for multi-dice 1.0.1
File Size Uploaded
multi_dice-1.0.1.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for multi-dice 1.0.1
File Interpreter ABI Platform
multi_dice-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / multi_dice-1.0.1.tar.gz

Download URL multi_dice-1.0.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c08e778c632a75cc804134ef79ad78f5c3890169717ce9677ef787fd85ab9dd0
BLAKE2b-256 checksum
How to use checksums
a08be1e5de73c4c66eb3588000b0adcb31e15391bbce35154002346e77f1d162
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release files / multi_dice-1.0.1-py3-none-any.whl

Download URL multi_dice-1.0.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e66fbd40e9a394225ed64c4e2253f86a86ad664f6f8565d535fc0dae2b0d1f8
BLAKE2b-256 checksum
How to use checksums
fbd2a8cee784fdcbb8fce83dfc26b606c1e5e2d25ac623b689851bb393918455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page