[Haskell-cafe] Help with IO and randomR

Niko Korhonen niko.korhonen at gmail.com
Mon Jul 16 07:40:08 EDT 2007

I'm writing some code to generate a dither (=noise) signal. I'm trying
to generate an infinite series of noise with triangular distribution but
my code hangs into an infinite loop. The problem is that I'm not very
good with Haskell IO yet and I can't figure out how to write this piece
of IO code without it looping infinitely.

So, in short, how do I do this without getting into an infinite loop:

tpdfs :: (Int, Int) -> IO [Int]
tpdfs (low, high) = do
  first <- getStdRandom (randomR (low, high))
  second <- getStdRandom (randomR (low, high))
  let r = (first + second) `div` 2
  rest <- tpdfs (low, high)
  return (r : rest)

Caller site:

  nums <- tpdfs (2, 12)
  let ns = take 7 nums


More information about the Haskell-Cafe mailing list