Skip to main content

Simple dice manipulation library allowing to compute probabilities of complex dice rolls

Project description

dice-checker

Release Build status codecov Commit activity License

A simple library to compute probabilities with dice rolls.

Roll objects can be created from a simple RPG-like string expression:

from dice_checker import Roll

Roll("D6")          # One 6 faced dice
Roll("2D8+1")       # Two 8 faces dices plus 1
Roll("5D10-1D6-1")  # Five 10 sided dices minus one 6 sided dice minus 1
Roll("1")           # A constant value of 1

Rolls can also be build from a custom map of value/probability

# Using a 6 faces dice where 1 to 4 gives 0 points, a 5 gives 1 point, a 6 gives 2 points
dice = Roll({0:4, 1:1, 2:1})

Rolls can also be built on the fly

from dice_checker import Roll

dice = Roll()

dice.add_event(event=1, probability=1)
dice.add_event(event=2, probability=1)

Rolls have two attributes:

  • distribution returns a map of value/chances.
  • expected_value returns the average value of the roll.
>>> from dice_checker import Roll
>>> roll = Roll("2D6+1")
>>> print(roll.distribution)
{3.0: 1.0, 4.0: 2.0, 5.0: 3.0, 6.0: 4.0, 7.0: 5.0, 8.0: 6.0, 9.0: 5.0, 10.0: 4.0, 11.0: 3.0, 12.0: 2.0, 13.0: 1.0}
>>> print(roll.expected_value)
8.0

Rolls have a normalized function that turns a new Roll with the probabilities scaled so their sum equals the requested value

>>> roll = Roll("D10")
>>> print(roll.distribution)
{1.0: 1.0, 2.0: 1.0, 3.0: 1.0, 4.0: 1.0, 5.0: 1.0, 6.0: 1.0, 7.0: 1.0, 8.0: 1.0, 9.0: 1.0, 10.0: 1.0}

# The sum of probabilities equals one.
>>> print(roll.normalized().distribution)
{1.0: 0.1, 2.0: 0.1, 3.0: 0.1, 4.0: 0.1, 5.0: 0.1, 6.0: 0.1, 7.0: 0.1, 8.0: 0.1, 9.0: 0.1, 10.0: 0.1}

# To get probabilitied as percentages
>>> print(roll.normalized(value=100).distribution)
{1.0: 10.0, 2.0: 10.0, 3.0: 10.0, 4.0: 10.0, 5.0: 10.0, 6.0: 10.0, 7.0: 10.0, 8.0: 10.0, 9.0: 10.0, 10.0: 10.0}

Rolls can be added and subtracted

>>> from dice_checker import Roll
>>> assert Roll("3D6") == Roll("2D6") +Roll("1D6")
>>> assert Roll("2D6-D6") == Roll("2D6") - Roll("1D6")

Rolls implement comparison operators that will return a new Roll containing the probability of the True/False events:

>>> from dice_checker import Roll
>>> (Roll("1D10") >= Roll("1D6")).distribution
{1.0: 45.0, 0.0: 15.0}

>>> (Roll("1D10") < Roll("1D20")).expected_value
0.725

Rolls implement a roll method that will returns random values according to their probabilities

>>> result = Roll("1D6").roll()

And finally, Rolls implement to_figure method that returns a Matplotlib Figure that can be saved as an image

>>> Roll("5D6").to_figure().savefig("images/5D6.png")

5D6

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dice_checker-0.0.4.tar.gz (130.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dice_checker-0.0.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file dice_checker-0.0.4.tar.gz.

File metadata

  • Download URL: dice_checker-0.0.4.tar.gz
  • Upload date:
  • Size: 130.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.19

File hashes

Hashes for dice_checker-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d755d203f8d5e58f71308af6393b8eabdcf235cb38eae22ff2b425d7d25b69f1
MD5 cc5d8a7ede37e6935a81b803fbacf75b
BLAKE2b-256 b65f5e562a5c1c04831419fba49de2d8e416b5887b90e51443679ab4cc668d91

See more details on using hashes here.

File details

Details for the file dice_checker-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for dice_checker-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fd8d90d21cb6af428a0899c9cd830ac94f8ee9741f99f7055bc45c2b33b9c818
MD5 95cc60c8030525acc09fb57df7b9742c
BLAKE2b-256 ec65e9682ae31c9db272e58be99835f304cec2ad0ac6514d87c3a80e169de89d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page