title: Random number generation in OCaml
body: <p>When using strict functional languages you are bound to a way of writing programs. I come with the problem of generating large quantity of pseudo random numbers with OCaml and I'm not sure I'm using the best way to generate this numbers on such language.</p>
  
  <p>What I did was create a module with a function (<em>gen</em>) that takes an integer as the <em>size</em> and an empty list and returns a list of pseudo random numbers of size <em>size</em>. The problem is when the size is to large, it asserts a <strong><em>StackOverflow</em></strong> which is what is expected. </p>
  
  <p>Should I use tail recursion? Should I use a better method that I'm not aware of? </p>
  
  <pre><code>module RNG =
  struct
    (* Append a number n in the end of the list l *)
    let rec append l n =
      match l with
      | [] -&gt; [n]
      | h :: t -&gt; h :: (append t n)
  
    (* Generate a list l with size random numbers *)
    let rec gen size l =
      if size = 0 then
        l
      else
        let n = Random.int 1000000 in
        let list = append l n in
        gen (size - 1) list
  end
  </code></pre>
  
  <p>Testing the code to generate a billion pseudo random numbers returns:</p>
  
  <pre><code># let l = RNG.gen 1000000000 [];;
  Stack overflow during evaluation (looping recursion?).
  </code></pre>