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

Daniel Fischer daniel.is.fischer at web.de
Fri Nov 26 06:16:14 EST 2010

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?
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

> What am I missing here?
> Best,
> Jan

More information about the Beginners mailing list