By the way, although a comment in the program says so,
other than the name, ran2 certainly isn't the ran2
algorithm in Numerical Recipes.
If you want to use a random number generator from Num. Rec.,
the books and the algorithms are available online
(ran2 is on p. 272, ch. 7, Numerical Recipes in Fortran 77 or 90):

As a general suggestion, you may get fewer bugs in Fortran if you drop
all implicit variable declarations in the program code
and replace them by explicit declarations
(and add "implicit none" to all program units, to play safe).
Implicit variable declarations are a big source of bugs.