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

编程入门 行业动态 更新时间:2024-10-28 06:33:07
本文介绍了如何从Haskell的列表中生成元素的随机序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I've had a look through hackage.haskell/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的列表中生成元素的随机序列?

本文发布于:2023-11-30 06:26:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   元素   列表中   Haskell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!