from bisect import bisect_left def rl(array): return range(len(array)) def fastIndex(a, x): i = bisect_left(a, x) return i class DtDTabulator(): def __init__(this, x, y, explosion=10, reroll=0): this.x = int(x) this.y = int(y) this.kaboom = int(explosion) this.reroll = int(reroll) this.paths = 10-this.reroll this.activeRolls = [([(0, True)]*this.x,1.0)] this.probabilities = [0]*1000 this.done = 0 #the largest value whose probability is finished being calculated def sumRoll(this, roll): return sum([die[0] for die in roll[-this.x:]]) def iterate(this, log = False): '''roll all die that need to be rolled, splitting them off into all possibilities''' newRolls = [] for roll in this.activeRolls: branchedRolls0 = [roll] for i in range(this.x): if roll[0][i][1]: branchedRolls1=[] for broll in branchedRolls0: for d10 in range(this.reroll+1, 11): branchedRolls1.append((broll[0][:], broll[1]/this.paths)) branchedRolls1[-1][0][i] = (broll[0][i][0]+d10, d10 >= this.kaboom) branchedRolls0 = branchedRolls1 newRolls.extend(branchedRolls0) print 'Rolled %i rolls, resulting in %i new rolls.'%(len(this.activeRolls), (len(newRolls))) '''move all rolls that still need to be rolled into active rolls, and tabulate those that have stopped exploding''' this.activeRolls = [] for roll in newRolls: roll[0].sort(key=lambda die: die[0]) isActive = False for die in roll[0]: isActive = isActive or die[1] if isActive: this.activeRolls.append(roll) else: this.probabilities[this.sumRoll(roll[0])] += roll[1] print 'Determined that %i of those rolls are still exploding.'%len(this.activeRolls) '''consolidate identical rolls and calculate the new this.done''' rollRef = [] probRef = [] for roll in this.activeRolls: i = fastIndex(rollRef, roll[0]) if i<len(rollRef) and rollRef[i] == roll[0]: probRef[i] += roll[1] else: rollRef.insert(i, roll[0]) probRef.insert(i, roll[1]) this.activeRolls = [(rollRef[i], probRef[i]) for i in rl(rollRef)] this.done = min([this.sumRoll(roll) for roll in rollRef]) print 'Consolidated that number into %i unique rolls, with probabilities now calculated up to %i.'%(len(this.activeRolls), this.done) x = DtDTabulator(5,4) x.iterate(log=True) x.iterate(log=True)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question