A Stochastic Encounter

I use gfortran fortran compiler at home and intel fortran compiler in office.

Its by random chance I found a vital difference in gfortran and intel compiler’s interpretation of using the intrinsic subroutine Random_number.

In gg95 fortran forum, someone posted this query.

I have the following code to generate a random number in between 0 and 1.

program random
implicit none
real :: R
call random_seed()

call random_number(R)
end program

But it always returns a same number. Why this happens, can any body suggest me?

I checked this in gfortran and I got the same result. Everytime it returned the same random number.

With Intel compiler the result was as expected. In sun solaris it was as expected. The program gave different random number on each call.

But gfortran was different. I thought it was a bug in gfortran.

But then Tobius Burnus explained this

No, it’s actually a bug in the Fortran standard: The standard does not
specify whether calling “random_number()” should produce by default the same sequence or every time a different sequence.

There are proponents for both.

One group argues that having a random_number() function should produce random results. Others argue that when running, e.g., an Monte Carlo algorithm, by default the results should be reproducible. As the standard does not mandate either choice, one has to live with having compiles in both groups. (I heard that the person responsible for adding the intrinsic believed that he had
written down the choice – but obviously he hasn’t.)

I think its worth keeping in mind. This can help solve lot of pain if you are using gfortran and intel compilers for your stochastic programs.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s