Who needs physical dice for D&D when you have a computer and recursion? I wrote a function that will roll a dice with m sides n times. The input is ndm. So, in order to roll 3d20 or 5d6, all you have to do is call
roll(3, 20, 0) or
roll(5, 6, 0), respectively.
Of course, the function above returns the sum of all of the dice rolls. So, what if we want to list out each individual roll? No problem — I also wrote another function called
bonus_roll (also recursive) that handles this for us. To roll 3d20 or 5d6, you’ll call
bonus_roll(3, 20, 0, new List()) or
bonus_roll(5, 6, 0, new List()), respectively.
This isn’t very succinct or pretty like
roll() and it is by no means efficient, but it gets the job done and it’s fairly readable.