Last active
December 20, 2015 22:29
-
-
Save yipyip/6205271 to your computer and use it in GitHub Desktop.
rerolling...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
#### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Danke Dirk ! Werd ich einbauen...