Re: newbie:: getting random Ints
[EMAIL PROTECTED] (Ketil Z. Malde) writes: snip OP Depending on why you need random numbers, you might want to consider using a fixed seed, i.e. a = randoms (mkStdGen 4711) will get you a sequence of random numbers. Of course, it will be the same sequence for every run of your program, but in some cases, (when you only want arbitrary data, and not necessarily random) this won't matter much. The advantage is, of course, that you get rid of the IO monad. I've toyed with the idea of using unsafePerformIO (at least to get the seed), but haven't quite dared (or needed) to yet. :-) well, thanks to all for the help. in my case, it was not OK to have arbitrary data, i needed (pseudo-) random numbers for different runs of the program. i want to: -generate random Ints -do some arbitrary computations on them to generate a [[Int]] -compare each [Int] in the list with a list of [Int] known at compile time the functions below seem to be doing what i need; i'm posting the code in case it helps other newbies get there a bit faster than i did. any constructive criticism / pointing out of errors most welcome. code: import Random rollDice :: IO Int rollDice = getStdRandom (randomR (1,6)) getRandomSeed :: IO Int getRandomSeed = do retval - rollDice return retval getRandomSeedInt :: IO Int - Int getRandomSeedInt x = unsafePerformIO x getARange :: Int - Int - [Int] getARange x y = randomRs (x,y) (mkStdGen (getRandomSeedInt getRandomSeed)) getRandomInt :: Int - Int getRandomInt x = head (take 1 (getARange 0 x )) output: Main take 20 (getARange 0 10) [5,8,1,8,2,8,9,7,1,4,6,2,5,8,6,2,10,0,7,10] Main take 20 (getARange 0 10) [9,8,9,9,7,2,4,5,1,7,2,2,8,2,5,10,5,3,1,8] thanks and regards, peter ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: newbie:: getting random Ints
the function randomRIO :: (a,a) - IO a defined in module Random gives you a random number between the two input parameters (including themselves). -- Andre - Original Message - From: Peter Rooney [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, March 28, 2002 3:05 AM Subject: newbie:: getting random Ints hello all, total newbie to haskell, armed with the craft of functional programming. i can't seem to generate random numbers. i have read this: http://www.haskell.org/onlinelibrary/random.html and searched the web and archives, and read thru Random.hs (mostly over my head), but have been unable to get any combination of getStdRandom randomR, etc. to work. even the example in the 98 report, import Random rollDice :: IO Int rollDice = getStdRandom (randomR (1,6)) gets me: Main rollDice Main after loading the file, which makes me think i'm missing something! TCFP has an example of how to do it yourself, but i can see that my needs are more than met by Random.hs. my various attempts all look a lot like this: Main getStdGen (random 5) ERROR - Type error in application *** Expression : getStdGen (random 5) *** Term : getStdGen *** Type : IO StdGen *** Does not match : a - b could someone post an example of how to generate random integers within a range? tia, peter ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
newbie:: getting random Ints
hello all, total newbie to haskell, armed with the craft of functional programming. i can't seem to generate random numbers. i have read this: http://www.haskell.org/onlinelibrary/random.html and searched the web and archives, and read thru Random.hs (mostly over my head), but have been unable to get any combination of getStdRandom randomR, etc. to work. even the example in the 98 report, import Random rollDice :: IO Int rollDice = getStdRandom (randomR (1,6)) gets me: Main rollDice Main after loading the file, which makes me think i'm missing something! TCFP has an example of how to do it yourself, but i can see that my needs are more than met by Random.hs. my various attempts all look a lot like this: Main getStdGen (random 5) ERROR - Type error in application *** Expression : getStdGen (random 5) *** Term : getStdGen *** Type : IO StdGen *** Does not match : a - b could someone post an example of how to generate random integers within a range? tia, peter ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe