gpt4 book ai didi

haskell - 如何从 Haskell 中的列表生成随机元素序列?

转载 作者:行者123 更新时间:2023-12-04 10:12:02 25 4
gpt4 key购买 nike

我看过https://hackage.haskell.org/package/random-1.1/docs/System-Random.html但是我看不到如何使用自定义“列表”,例如 ['a'..'z'] ++ ['0' .. '9'] 的字母数字列表?

我想作为一种解决方法,我可以改为映射一组随机数字。

最佳答案

我提到的工作的实现:

Prelude> import System.Random
Prelude System.Random> gen <- newStdGen
Prelude System.Random> x = ['a'..'z'] ++ ['0' .. '9']
Prelude System.Random> fmap (x !! ) (take 10 $ randomRs (0, length x - 1 ) gen)
"h4tm52rfox"

关于haskell - 如何从 Haskell 中的列表生成随机元素序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com