# [Haskell-beginners] Random computation with fixed seed value (Control.Monad.Random)

Jan Snajder jan.snajder at fer.hr
Fri Nov 26 06:51:11 EST 2010

```Daniel,

On Fri, 2010-11-26 at 12:16 +0100, Daniel Fischer wrote:
> On Friday 26 November 2010 08:20:38, Jan Snajder wrote:
> > Hi,
> >
> > I'd like to evaluate a random computation using Control.Monad.Random,
> > but I'd like to be able to fix the seed value. I know I can use mkStdGen
> > from System.Random to get an initial generator. I also know I can use
> >
> > evalRand :: RandomGen g => Rand g a -> g -> a
> >
> > from Control.Monad.Random to evaluate a random computation. But the
> > problem is I don't see how I can turn a StdGen value that I get from
> > mkStdGen into a Rand type.
>
> What would you need that for?

Solely for illustrative purposes. I need to compare a result of a random
computation when I run it with different parameters.

> Rand is basically the State monad (newtype wrapped), which is basically
>
> s -> (a,s)
>
> (newtype wrapped).
>
> You can write your computation so that it works with whatever pseudo-random
> generator it is given and then let g be determined by the value passed to
> evalRand. That is the default use case.
>
> For example
>
> die :: (RandomGen g) => Rand g Int
> die = getRandomR (1,6)
>
> dice :: (RandomGen g) => Int -> Rand g [Int]
> dice n = sequence (replicate n die)
>
> main :: IO ()
> main = do
>   print \$ evalRand (dice 10) (mkStdGen 123)
>   print \$ evalRand (dice 10) (mkBlumBlumShub 2379009 1234567890983)
>
> (for a hypothetical
>
> data BlumBlumShub = BBS !Integer !Integer
>
> instance RandomGen BlumBlumShub where ...
>
> mkBlumBlumShub :: Integer -> Integer -> BlumBlumShub
> )

Great, thanks!

I tried the other way around:

evalRand (mkStdGen 123) (dice 10)

and kept scratching my head wondering why it doesn't work. :-)
I guess it was too early in the morning.

Best,
Jan

```

More information about the Beginners mailing list