Skip to content

Instantly share code, notes, and snippets.

@jpverkamp
Last active December 16, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpverkamp/5415536 to your computer and use it in GitHub Desktop.
Save jpverkamp/5415536 to your computer and use it in GitHub Desktop.
Average value and success chance for d4 + wild in Savage worlds
import random
# Roll an n-sided die
def d(n):
return random.randint(1, n)
# Roll an n-sided die, exploding on maximum value
def e(n):
total = 0
while True:
roll = d(n)
total += roll
if roll != n:
break
return total
# Roll an exploding n-sided die and an exploding d6, take the better
def w(n):
return max(e(n), e(6))
# Roll 1 million times exploding d4+wild
print 'd4 + wild:'
dice = ([w(4) for i in xrange(1000000)])
print '\tavg:', 1.0 * sum(dice) / len(dice)
print '\tsuc:', 1.0 * len(filter(lambda n : n >= 4, dice)) / len(dice)
# Roll 1 million times just exploding d4
print 'd4:'
dice = ([e(4) for i in xrange(1000000)])
print '\tavg:', 1.0 * sum(dice) / len(dice)
print '\tsuc:', 1.0 * len(filter(lambda n : n >= 4, dice)) / len(dice)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment