Code Monkey home page Code Monkey logo

dndice's Introduction

DnDice

This is a module that calculates and prints value-probabiliy distributions of dice rolls as commonly used in tabletop roleplay games such as 'Dungeon & Dragons®' (I haven't actually looked at what games other than D&D 5e might require.)

Examples

This shows the basic dice rolling syntax, and how the probabilities are expressed. Notice that the +5 inside the gwf function is stripped:

from DnDice import d, gwf

single_attack = 2*d(6) + 5
print(single_attack) # dice: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

# don't do this:
wrong_way = gwf(2*d(6) + 5)
print(wrong_way) # dice: [2, 3, 4, 5, 6, 7, 8, ,9, 10, 11, 12]
# do this instead
great_weapon_fighting = gwf(2*d(6)) + 5
print(great_weapon_fighting) # dice: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

# comparison of the probability
print(single_attack.expectancies())
print(great_weapon_fighting.expectancies())
# [ 0.03,  0.06, 0.08, 0.11, 0.14, 0.17, 0.14, ...] (single attack)
# [0.003, 0.006, 0.03, 0.05, 0.10, 0.15, 0.17, ...] (gwf attack)

plot((single_attack, 'normal 2d6'), (great_weapon_fighting, '2d6 with Great weapon fighting feat'))

graph of 2d6 vs 2d6 with Great weapon fighting feat

If you have cases where the dice rolled are dependent on a separate roll:

from DnDice import d, advantage, plot

normal_hit = 1*d(12) + 5
critical_hit = 3*d(12) + 5

result = d()
for value, probability in advantage():
	if value == 20:
		result.layer(critical_hit, weight=probability)
	elif value + 5 >= 14:
		result.layer(normal_hit, weight=probability)
	else:
		result.layer(d(0), weight=probability)
result.normalizeExpectancies()

plot(
	(normal_hit, "normal hit"),
	(critical_hit, "critical hit"),
	(result, "weighted by an advantage d20")
)

graph of normal and critical hit, and their weighted probabilities

dndice's People

Contributors

lordsembor avatar pimoll avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.