Skip to content

Instantly share code, notes, and snippets.

@yipyip
Last active December 20, 2015 22:29
Show Gist options
  • Save yipyip/6205271 to your computer and use it in GitHub Desktop.
Save yipyip/6205271 to your computer and use it in GitHub Desktop.
rerolling...
import random as rand
####
def reroll_rec(minval=1, maxval=6, accu=0, depth=99):
"""It's tailrecursion when a function don't have to go back the whole call stack.
Only for demonstration purpose. Not intended for production use.
"""
assert depth >= 0
result = rand.randint(minval, maxval)
if result < maxval or depth == 0:
return accu + result
return reroll_rec(minval, maxval, accu + result - 1, depth - 1)
####
def reroll(minval=1, maxval=6, depth=99):
"""Loop version.
"""
assert depth >= 0
accu = 0
while True:
result = rand.randint(minval, maxval)
if result < maxval or depth == 0:
return accu + result
accu += result - 1
depth -= 1
####
@horstjens
Copy link

Danke Dirk ! Werd ich einbauen...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment