Make the instance generator select proposal topics from a Zipf distribution, and
[match/match.git] / program / RandomizedMonad.hs
index 62e8808..22b98aa 100644 (file)
@@ -3,7 +3,7 @@ module RandomizedMonad (
        msplit,
        runRandom1, runRandom, runRandomStd, runRandomNewStd,
        mrandomR, mrandom,
-       withProb,
+       withProb, withWeight,
        filterRandomized,
        indReplicateRandom, indRepeatRandom, indRandomArray
 ) where
@@ -70,6 +70,13 @@ withProb ifCs elseR = do
        val <- mrandom
        chooseCase val ifCs elseR
 
+-- Like withProb, but without an else case and with the "probabilities" scaled
+-- so that they sum to 1.
+withWeight :: [(Double, Randomized a)] -> Randomized a
+withWeight ifCs = do
+       val <- mrandomR (0, sum (map fst ifCs))
+       chooseCase val (tail ifCs) (snd (head ifCs))
+
 -- Keep trying until we get what we want.
 filterRandomized :: (a -> Bool) -> Randomized a -> Randomized a
 filterRandomized f ra = do