如何调用此Haskell函数? [英] How does one call on this Haskell function?

查看:50
本文介绍了如何调用此Haskell函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个函数,用于生成一定范围内给定数量的随机数的列表,但是我对如何调用此函数感到困惑.我认为StdGen需要种子,但是如果有人可以告诉您如何精确调用此函数,我将非常感激.

  randomList ::(随机数a)=>(a,a)->整数->StdGen->[一种]randomList bnds n =取n.randomRs出价 

解决方案

示例:

  main :: IO()主=做g<-getStdGen打印(randomList(1,100)5 g :: [Integer]) 

将输出例如

  [42,42,15,7] 

g 是随机数生成器,在这种情况下是全局随机数生成器".这是文档

This is a function to generate a list of given number of random numbers within a range, but I'm confused on how to call on this function. I think a seed is needed for StdGen, but really appreciate if someone can tell how exactly to call this function.

randomList :: (Random a) => (a,a) -> Int -> StdGen -> [a]
randomList bnds n = take n . randomRs bnds

解决方案

Sample:

main :: IO ()
main = do g <- getStdGen
          print (randomList (1, 100) 5 g :: [Integer])

will output e.g.

[42,42,15,7]

The g is the random number generator, in this case the "global random number generator". Here's the documentation

这篇关于如何调用此Haskell函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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