Skip to content

Instantly share code, notes, and snippets.

@weatherspud
Created December 11, 2016 13:58
Show Gist options
  • Save weatherspud/f8755d176a68e177e84e15bd2f862c06 to your computer and use it in GitHub Desktop.
Save weatherspud/f8755d176a68e177e84e15bd2f862c06 to your computer and use it in GitHub Desktop.
approximate a fireball roll with two dice
#!/usr/bin/env python3.5
import math
die = 6
mean = sum(range(1, die + 1)) / die
variance = sum([i * i for i in range(1, die + 1)]) / die - mean ** 2
max_roll = 30
for n in range(1, max_roll + 1):
best_diff = None
best_b = None
for b in range(0, math.ceil(math.sqrt(n - 1)) + 2):
if n % 2 == b % 2:
continue
diff = abs(b**2 + 1 - n)
actual_sd = math.sqrt(n * variance)
approx_sd = math.sqrt(b * b * variance + variance)
# print('DEBUG n: {} b: {}: diff: {}'.format(n, b, diff))
if best_diff is None or diff < best_diff:
best_b = b
best_diff = diff
best_actual_sd = actual_sd
best_approx_sd = approx_sd
a = int(mean * (n - best_b - 1))
sd_change = best_approx_sd - best_actual_sd
print(u'{}d6: {} + d6 × {} + d6'.format(n,
a,
best_b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment