Skip to content

Instantly share code, notes, and snippets.

@hannahwhy
Created September 9, 2009 18:49
Show Gist options
  • Save hannahwhy/183966 to your computer and use it in GitHub Desktop.
Save hannahwhy/183966 to your computer and use it in GitHub Desktop.
--
-- calculate sequence of die rolls needed to hit some probability target;
-- most useful in conjunction with take, e.g.
--
-- take 25 (dieProb 12 1%7)
--
-- originally written by me @ http://guyblade.livejournal.com/159423.html, and archived here
--
module DieProb where
import Ratio
dieProb sides target = map numerator $ dp sides target 1 1
dp sides target power count =
let weight = 1 % (sides ^ power) in
if target == 0 then
[]
else
if (target <= (count * weight)) && (target > ((count - 1) * weight)) then
let newWeight = (count * weight) - target
newPower = power + 1
in count:(dp sides newWeight newPower 1)
else
dp sides target power (count + 1)
isFailure seq sides target = any (uncurry (>)) $ zip seq (dieProb sides target)
isSuccess = ((.).(.).(.)) not isFailure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment