// e.g. roll(2).d(8) + roll().d(4) + 11
const roll = (n = 1) => ({
d: (sides) =>
Array.from({ length: n }, () => Math.ceil(Math.random() * sides)).reduce(
(x, y) => x + y,
0
),
})
class Integer
# monkey patch for rolling virtual dice
# e.g. 2.d8 + 1.d4 + 11
def method_missing(name)
if name[/^d\d+$/]
n = name[1..-1].to_i
self.times.map { rand(1..n) }.sum
end
end
end
from random import randrange
class Roll(object):
"""
e.g. Roll(2).d(8) + Roll().d(4) + 11
"""
def __init__(self, n=1):
self.n = n
def d(self, sides):
return sum([randrange(1, sides) for _ in range(1, self.n)])