ran2 problems (bug???)


bgbg
03-16-2004, 10:33 AM
I've copied the ran2 function from http://lib-www.lanl.gov/numerical/bookcpdf/c7-1.pdf
.
I use time(NULL) as initial seed.
For some reason first eight numbers are totally equal from one run to another. There are some more equal nubers in other places.
I test the program with the following commands (on linux)
$ ./ran2test > one; sleep 1;
$ ./ran2test >two;
$ paste one two | awk -F' ' '{print $1-$2}' | cat -n
These commands generate two different sequences and then compare the corresponding elements.

I've attached a text file that contains the ran2 function, the test program and some outputs.
Can someone comment on this ?

Saul Teukolsky
03-18-2004, 02:32 PM
It turns out bgbg forgot to initialize ran2 with a negative seed.

bgbg
03-19-2004, 02:48 PM
it was a classic RTFM mistake. Sorry.