[Haskell-cafe] setting seed for random numbers

Daniel Fischer daniel.is.fischer at web.de
Sun Jan 3 13:29:52 EST 2010

Am Sonntag 03 Januar 2010 18:46:30 schrieb Larry E.:
> I want to set a random seed for a random number generator. I tried
> this:
> getSeed = do
>        time <- getClockTime
>        return (ctPicosec (toUTCTime time))
> seed  <-  getSeed
> randList minval maxval = randomRs (minval,maxval) (mkStdGen seed)
> But I get the error:     parse error on input `<-' which refers to
> "seed<-getSeed".
> Using "seed<-getSeed"  from the  ghci command line works. Why doesn't
> it work in a
> script?

    value <- action

syntax is only available in do-blocks.
At the ghci prompt, you are in an IO do-block, so it's available there. In a script 
(source file), you are not.

I suggest making the generator an argument of randList, getting the seed and from that the 
generator in main and pass the generator to the pure functions doing the work.

Also, it might be good to use the random monad, available from 

> THe second problem is the seed is of type Integer but mkStdGen wants
> an Int. How can I get an Int from an Integer? I tried mod to get the
> seed to within range for an int but the result is still of type
> Integer how can I coerce Integer to Int when I know result will fit?


> Thanks

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100103/9de40230/attachment.html

More information about the Haskell-Cafe mailing list