如何在递归中使用随机数?

时间:2014-06-11 08:51:41

标签: haskell recursion random

尝试从列表中选择一个随机元素但是#39; ll使函数不纯,因此无法编译。如何使递归函数接受IO操作?

build :: Jabberwocky Integer String Syllables -> String
build (Jabberwocky 0 body syl) = body
build (Jabberwocky len body syl)
    | syl == Middle     = build (Jabberwocky (len - 1) (body ++ (rand middle)    ) Consonant)
    | syl == Consonant  = build (Jabberwocky (len - 1) (body ++ (rand consonant)) Vowel)
    | syl == Vowel      = build (Jabberwocky (len - 1) (body ++ (rand vowel)     ) Consonant)
    | syl == Ending     = build (Jabberwocky (len - 1) (body ++ (rand ending)    ) Vowel)
        where 
            rand = getStdRandom (randomR (1,6))

1 个答案:

答案 0 :(得分:2)

你必须将发电机带入纯过程(链接新的随机发电机状态)

randomR_nTimes_rec :: (RandomGen g, Random a) => Int -> (a, a) -> g -> ([a], g)
randomR_nTimes_rec 0 _ g = ([], g)
randomR_nTimes_rec n i g = (x:xs, g'') where ( x, g' ) = randomR i g
                                             (xs, g'') = randomR_nTimes_rec (n - 1) i g'

使用

*Main> getStdGen  >>= return . randomR_nTimes_rec 5 (0,5)
([2,5,3,1,3],1206240749 652912057)

如果您将随机状态带入复杂流程可能会有用Control.Monad.Random with example