Simple Haskell Monad - 随机数 [英] Simple Haskell Monad - Random Number

查看:160
本文介绍了Simple Haskell Monad - 随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扩展这篇文章中的代码(接受的答案),以便让我能够根据函数randomGen调用randomGen2来获得一个随机数,该函数将种子作为参数。但每次我调用randomGen2,尽管返回一个Int,我得到一个关于不能打印随机的错误(当我试图打印Int时)。

 < interactive>:3:1:
(Show(Random Int))
使用`print'
可能的修正:为(Show(Random Int))添加一个实例声明
在一个交互式GHCi命令中:print it


$ b

以下是代码:

  import Control.Monad.State 

type Seed = Int

randomGen :: Seed - > (种子,种子)
randomGen种子=(种子,种子+ 1)

类型随机a =状态种子a

randomGen2 :: Random Int
randomGen2 = do
seed< - get
let(x,seed')= randomGen seed
放入种子'
return x

有什么想法?

更新 - 预期行为 $ b我希望能够从翻译人员那里做些什么(例如GHCI) -

 > getRandomInit 1 
> - 其他一些东西,例如2 + 3
> getRandom

即我可以使用种子设置我的getRandom函数,然后使用 put 存储种子,然后此函数返回并退出。然后我做了一些其他的事情,然后再次调用getRandom,并从Monad中取回它的种子。 解决方案

randomGen2 返回一个 Random Int ,它是一个 State Seed Int 所以你需要使用 runState 来获得一个结果,通过提供一个种子值例如

  runState randomGen2 1 

如果您想继续重复使用 randomGen2 您可以创建一个函数,如:

  genRandoms :: Seed  - > [int] 
genRandoms s = let(v,s')= runState randomGen2 s in v:genRandoms s'

或者您可以使用序列

  genRandoms ::种子 - > [Int] 
genRandoms s = fst $ runState(sequence(repeat randomGen2))s


I'm trying to extend the code in this post (accepted answer) to allow me to be able to call randomGen2 to get a random number, based on the function randomGen which takes a seed as an argument. But everytime I call randomGen2, despite returning an Int, I get an error about not been able to print Random (when infact I am only trying to print Int).

<interactive>:3:1:
    No instance for (Show (Random Int))
      arising from a use of `print'
    Possible fix: add an instance declaration for (Show (Random Int))
    In a stmt of an interactive GHCi command: print it

Here's the code:

import Control.Monad.State

type Seed = Int

randomGen :: Seed -> (Int, Seed)
randomGen seed = (seed,seed+1)

type Random a = State Seed a

randomGen2 :: Random Int
randomGen2 = do
        seed <- get
        let (x,seed') = randomGen seed
        put seed'
        return x

any ideas?

Update - expected behaviour I'm hoping to be able to do something this from an interpreter (e.g. GHCI) -

> getRandomInit 1
> -- some other stuff, e.g. 2 + 3
> getRandom

i.e. I can set up my getRandom function with a seed, which then stores the seed using put, then this function returns and exits. I then do some other stuff, then call getRandom again, and the seed is retrieved from the Monad where it was stored.

解决方案

randomGen2 returns a Random Int which is a State Seed Int so you'll need to use runState to get a result by providing a seed value e.g.

runState randomGen2 1

If you want to keep repeating the application of randomGen2 you can create a function like:

genRandoms :: Seed -> [Int]
genRandoms s = let (v, s') = runState randomGen2 s in v : genRandoms s'

or you could use sequence:

genRandoms :: Seed -> [Int]
genRandoms s = fst $ runState (sequence (repeat randomGen2)) s 

这篇关于Simple Haskell Monad - 随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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