NR in Fortran 90 random generators are different!


mathwiz
03-01-2002, 05:42 PM
I notice that the random number generators in the Fortran 90 version of Numerical Recipes are different from the generators in all the other versions (C, C++, and Fortran 77).

Why?

Which ones are better? I'm a C++ user. Should I port the Fortran 90 versions to C++ ?

Bill Press
03-01-2002, 06:23 PM
Mathwiz, those are great questions! You're not a shill are you? ;)

There are two reasons that the Fortran 90 generators for uniform random deviates are different from the other books:

1. Since both serial and parallel versions of each generator are provided in Fortran 90, all the methods used had to parallelize easily. In particular they had to use a relatively small state space per (parallel) component. This rules out the use of techniques like the Bays-Durham shuffle, or Knuth's subtractive method.

2. By the time we wrote the Fortran 90 book, some newer techniques had been discovered and tested extensively (e.g., by Marsaglia).

The C++, C, and Fortran 77 books put together uniform deviate routines out of these building blocks: Linear congruential generators or LCGs (e.g., Park and Miller's or L'Ecuyer's); Schrage trick to do long arithmetic; Bays-Durham shuffle; or Knuth's subtractive method. This is a powerful enough toolkit to construct an (essentially) perfect generator, as in ran2. But the operations count per random deviate is unpleasantly large.

The generators in the Fortran 90 book use a different, arguably more modern, toolkit, largely due to Marsaglia: Lagged Fibbonacci generators, "Marsaglia shift registers", use of more than one combining method, and very occasional use of LCGs. This toolkit can give (again, essentially) perfect generators with smaller operations counts.

Yes, the routines in the Fortran 90 book could easily be ported to C++ (with due attention that the assumed overflow/wraparound properties of 32-bit integers that are tested in the Fortran 90 ran_init work for your compiler). Yes, they should work fine, and be faster. To be safe, you might want to locate a Fortran 90 compiler (horrible thought!) just to test that your C++ implementation gives exactly the same random deviates as the Fortran 90 routines. If it doesn't, then your port is incorrect -- and this might matter a lot! ("Close enough" doesn't cut it with random generators!)

By the way, you don't have to buy the Fortran 90 book to see these routines. The book chapter is on the web at http://lib-www.lanl.gov/numerical/bookf90pdf/chap7f9.pdf

mathwiz
03-01-2002, 06:32 PM
So if the Fortran 90 generators are better algorithms, how come you guys didn't port them over to your newest version of the NR book, namely C++ ???

Just curious.

Bill Press
03-01-2002, 06:40 PM
Sigh! :o This was a tough choice. In the end, we decided that it was better to keep the synchronization between the C and C++ versions of NR.

Keep in mind that we know of nothing wrong with the ran routines in the C (and now C++) versions. They're just a bit clunky and tad inefficient.

We were imagining the screams from C users who might switch to C++ and suddenly find that the same-named routine produces different results. So, we left the C++ and C algorithmically identical.

Call it a life-style choice!

Haydyn
05-21-2002, 07:53 AM
If you want to have C/C++ versions of the random number algorithms developed by George Marsaglia, he has posted implementations of these in C to various news groups, sci.math etc, along with some discussion about them. This could save you some work converting the Fortran versions.

These can be found in the google groups. (groups.google.com) by following the link below

http://groups.google.com/groups?q=Random+Number+Generators+for+C+group:sci. math+author:Marsaglia&hl=en&lr=&selm=36A5BB98.F2561DFF%40stat.fsu.edu&rnum=6

or by searching for the keywords

Random Number Generators C Marsaglia group:sci.math


Haydyn