title: Sampling sequences of random numbers in Haskell
body: <p>I need small lists of gaussian random numbers for a simulation and so I tried the following:</p>
  
  <pre><code>import System.Random
  
  seed = 10101
  gen = mkStdGen seed
  
  boxMuller mu sigma (r1,r2) =  mu + sigma * sqrt (-2 * log r1) * cos (2 * pi * r2) 
  </code></pre>
  
  <p>This is just the Box-Muller algorithm - given r1, r2 uniform random numbers in the [0,1] interval it returns a gaussian random number.</p>
  
  <pre><code>normals 0 g = [] 
  normals n g = take n $ map (boxMuller 0 1) $ pairs $ randoms g
      where pairs (x:y:zs) = (x,y):(pairs zs)
  </code></pre>
  
  <p>So I used this <code>normals</code> function everytime I needed my list of random numbers. </p>
  
  <p>The problem with that must be apparent: it generates always the same sequence cause I'm using always the same seed! I'm not getting new sequences, I'm only getting the first n values of the sequence all the time.</p>
  
  <p>What I was pretending clearly was that, when I type:</p>
  
  <pre><code>x = normal 10 
  y = normal 50
  </code></pre>
  
  <p>I'd have x to be the first 10 values of <code>map (boxMuller 0 1) $ pairs $ randoms g</code> and y to be the next 50 values on this list, and so on. </p>
  
  <p>Of course this is impossible, cause a function must always return the same values given the same input. How do I escape this trap?</p>