How to seed the ramdom number generator

How to seed the ramdom number generator

Howdy,

I'm an instructor for a course using IFC on redhat linux. We recently had a project that involved doing a simulation,
using the built in random number generator.

We quickly found that RANDOM_SEED puts the generator in the same state on every run of the program. Not really what we wanted, so I tried to write a subroutine that, given a magic number would initialize the RNG with that number as a 'seed' (choosing 1 of 2**32 different sequences). I was not able to get this to work in IFC (I had no trouble with another fortran compiler on adifferent platform).

What I tried was this, and I'm baffled as to why it wouldn't work. I called RANDOM_SEED with size, to get the seed size; allocated an array of that size (which is 47!); filled it with a deterministic sequence based on the magic number, then called RANDOM_SEED with put to use that sequence to seed the random number generator.

The results were rather astounding. Subsequent calls to RANDOM_NUMBER sinply produced a sequence like x_new = x_old + .19, modulo 1.0. (.19 is not the actualy number, but is close to it as best I recall).

Investigating further, I did a call RANDOM_SEED with get after each call to RANDOM_NUMBER. I saw that the 47th value in the array was just increasing by .19/2**32. The other 46 numbers had two that were just decreasing by 1 on each call. The rest of them seemed to be filling up with -6.

Anyway, I feel one shouldn't expect to have to dig that deeply to seed a random number generator. I could have easily implemented a PRNG from scratch in the same amount of time. And if I don't get an answer, that's what I will do next semester. However, I still would like to understand what the correct way to do this is, in a manner that will work on any compiler, but especially on IFC.

Thanks,
Bob Harris

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I don't know any details about the way RANDOM_NUMBER works in ifc 7. However, in the upcoming 8.0, it adopts the Compaq Fortran version which is very good, has a seed length of 2, and has the behavior you want of giving a different seed each time you call RANDOM_SEED with no arguments.

Steve - Intel Developer Support

MADsblionel wrote:
> I don't know any details about the way RANDOM_NUMBER
> works in ifc 7. However, in the upcoming 8.0, it
> adopts the Compaq Fortran version which is very good,
> has a seed length of 2, and has the behavior you want
> of giving a different seed each time you call
> RANDOM_SEED with no arguments.

Thanks. Sounds like someone realized it was a problem. As I understand it, what the current version has *does* conform to the fortran standard. But I've never seen a PRNG that behaves that way.

In addition to being able to get a different sequence every time RANDOM_SEED is called with no arguments, the other desired use is to be able to specify a particular seed so that you can get repeatible sequences (but still be able to select different ones). For example, to be able to verify each student's results, I might tell them to use their student ID as the PRNG seed. Then I can reporduce the same sequence of numbers, yet every student has a different sequence.

Am not sure if version 8 will be available for the spring semester. But the more I think about it, the more I think I will need to do my own implementation of a PRNG, and give it to the class to use. A few students each semester use different compilers (so they can work at home instead of coming into the lab on campus), and by providing a platform-independent PRNG I will be able to get consistent results among the students.

Thanks for your info,
Bob H

The Fortran standard says only that calls to RANDOM_SEED with no arguments set the seed to a "processor-dependent value". Compaq Fortran initially set the seed to a fixed value, but we changed the behavior a few years ago.

Using your own RNG is not a bad idea, especially if the behavior of the RNG is important to your work. While many compiler-supplied RNGs are good, many are not so good, and compiler vendors often don't document them. The Compaq Fortran RANDOM_NUMBER is based on one from Pierre L'Ecuyer (if I have the spelling right...)

Steve - Intel Developer Support

Perhaps I am bit late in this discussion. But this is what I do in ifc 7.1
1. First write the random_seed into a file.
2. use this seed to initialize the required program.
3. when the program is about to exit, write the seed to the file (mentioned in step 1)

This method is portable irrespective of the compiler. The drawback is that there will be one additional file and 2 additional lines of code.

I have the code which does that. If u need it contact me at kk288@cornell.edu
Actually I think it will be available in google's archives.

Leave a Comment

Please sign in to add a comment. Not a member? Join today