Hi, Could someone please post a fragment of code that builds a list of random Doubles using RandomIO (recursively, please, although an example of using the interface that gives lists of random values directly would also be interesting)? ie Int -> IO [Random] Sorry - I've searched the web but can't find an example. I can get integers, but I don't udnerstand how to get Doubles. I've stared at the Library docs, but am missing something obvious, I guess. Thanks very much, Andrew -- http://www.andrewcooke.free-online.co.uk/index.html
Duh. Should have been Int -> IO [Double]. (Int being the length of the list - the list bit isn't really important, it's just getting doubles rather than ints) Andrew On Wed, Feb 14, 2001 at 04:25:04PM +0000, andrew@andrewcooke.free-online.co.uk wrote: [...]
ie Int -> IO [Random] [...]
Wed, 14 Feb 2001 16:25:04 +0000, andrew@andrewcooke.free-online.co.uk <andrew@andrewcooke.free-online.co.uk> pisze:
Could someone please post a fragment of code that builds a list of random Doubles using RandomIO (recursively, please, although an example of using the interface that gives lists of random values directly would also be interesting)?
Doubles from which range? Assuming (0,1) and that the Int means the length of the list: import Random import Monad rands1:: Int -> IO [Double] rands1 0 = return [] rands1 n = do x <- randomIO rest <- rands1 (n-1) return (x:rest) rands2:: Int -> IO [Double] rands2 0 = return [] rands2 n = liftM2 (:) randomIO (rands1 (n-1)) rands3:: Int -> IO [Double] rands3 n = sequence (replicate n randomIO) rands4:: Int -> IO [Double] rands4 n = do g <- newStdGen return (take n (randoms g)) -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (2)
-
andrew@andrewcooke.free-online.co.uk -
qrczak@knm.org.pl