title: generating random numbers in a Fortran Module
body: <p>Now I am facing the problem that in a <strong>module</strong>, with a seed I am generating random numbers to be used in a loop  of a function but each time I call that function, the same random numbers are generated (because <strong>the seed is obviously the same</strong>) but it's supposed that it must continue the series or at least it must be different between calls. One solution could be that the main program gives a new seed to be used in the module but I think it there could be another elegant solution.
  I am using <strong>Mersenne Twister</strong> generator by suggestion of many people.</p>
  
  <p><em>Added</em></p>
  
  <p>My function in my module (it is a package of functions) escentially makes such a Metropolis test using random numbers generated by a seed, for some reason compilation complains if I put</p>
  
  <pre><code>    module mymod
      uses mtmod
      call sgrnd(4357)!&lt;-- this line causes compilation error
      contains
      myfunc(args)
      implicit none
      // declarations etc
      !call sgrnd(4357) &lt;-- if I put this call here compilator says ok,
      !but re-start random number series each time this function is called :(
      ....
      !the following part is inside a loop
      if (prob &lt; grnd()) then
      !grnd() is random number generated
      return
      else continue testing to the end of the loop cycle
      end myfunc
  </code></pre>
  
  <p>But if I put that function in the contains of the main program (using mtmod too) and call sgrnd(4357) before contains section and the calls to myfunc, now everything compile and run nicely. For clarity, I didn't want to put that long function in the main program, it has 70 lines of code, but it seems I have no escape. Notice that so, the seed is once called. The simulations have now physical meanings but with that price payed.</p>