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

Hello Larry, Sunday, January 3, 2010, 8:46:30 PM, you wrote:
Using "seed<-getSeed" from the ghci command line works. Why doesn't it work in a script?
because it's a feature of ghci command prompt, not haskell language. use smth like this: randList minval maxval = do seed <- getSeed return$ randomRs (minval,maxval) (mkStdGen seed) of course, it will make randList non-pure function that may return different results on different calls but that that you mean, no? ;) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

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? The
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 http://hackage.haskell.org/package/MonadRandom
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?
fromInteger
Thanks
participants (3)
-
Bulat Ziganshin
-
Daniel Fischer
-
Larry E.