So this is due to stacking several dice-tricks (rote, 8-again, roll twice and take the best) in a game of Mage the Awakening and as a result the mechanics are wonky. the command is to roll an arbitrary number DICEPOOL of d10 dice. Then reroll every die with a result below 8. Once. Then for every die above 7, "Explode" by adding a new die and keep recursively exploding if the new die rolls above 7 Count the number of successes (dice above 7) Repeat the entire thing and take the better result. Or, to put it in psuedo-code int output = 0 do twice { int successCount = 0; Set<Dice> initialRollResult = rollD10s(DICEPOOL) Set<Dice> unexplodedDice = new Set<>() for each Dice initialDie in initialRollResult { if(initialDie <8) { initialDie.reRoll() } unexplodedDice.add(initialDie) } for each Dice unexplodedDie in unexplodedDice { while(unexplodedDie >7) { successCount = successCount +1 unexplodedDie.reroll() } } if(successCount>output) { output = successCount; } } return output