
Niko Korhonen wrote:
Bryan Burgers wrote:
tpdfs range = do g <- newStdGen -- get a random generator (g1, g2) <- return $ split g -- make two random generators out of it return $ zipWith combine (randomRs range g1) (randomRs range g2) -- get two streams of random numbers, and combine them elementwise.
combine x y = (x + y) `div` 2
So, moving on to the next question, how well do you think this solution would scale if we would need n random numbers to generate one?
As a rule of thumb, anything that works with two operands of the same type can be generalized to more than two operands, using lists and fold resp. unfold. The following code is untested and may contain errors, but you should get the idea: -- combine n generators by taking the arithmetic mean arithMean xs = sum xs `div` length xs -- must be a finite nonempty list! -- create infinitely many generators from one splitMany = map fst . iterate (split . snd) tpdfs range = do g <- newStdGen -- get a random generator gs <- return $ iterate splitMany g return $ map arithMean . map (randomRs range) (take 42 gs) Cheers Ben