如何从Haskell的列表中生成元素的随机序列? [英] How can I generate a random sequence of elements from a list in Haskell?

查看:53
本文介绍了如何从Haskell的列表中生成元素的随机序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了 https://hackage.haskell.org/package/random-1.1/docs/System-Random.html ,但是我看不到如何使用自定义列表",例如 ['a'..'z'] ++ ['0'..'9'] ?

I've had a look through https://hackage.haskell.org/package/random-1.1/docs/System-Random.html however I can't see how to use a custom "list" for example an alphanumeric list of ['a'..'z'] ++ ['0' .. '9']?

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

I suppose as a workaround I could instead map a random set of numbers instead.

推荐答案

我提到的实现工作的实现:

The implementation of the work around I mentioned:

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的列表中生成元素的随机序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆