Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Dynamic Random Number Generation

Pappu_M_
Beginner
2,842 Views
What should I do if I want dynamic random number generation, i.e. every time I use a fortran program that generates random sequences of numbers, it should generate different sequences. Currently the way I see it is always the same sequence. Thanks in advance.
0 Kudos
17 Replies
Les_Neilson
Valued Contributor II
2,843 Views

What random number generator are you using?
If you use the same number as a starting seed you will generate the same sequence of psuedo random numbers. (This is useful for testing pruposes)

If you are using theinbuiltRANDOM_NUMBER then, from the help :
"The seed for the pseudorandom number generator used by RANDOM_NUMBER can be set or queried withRANDOM_SEED. If RANDOM_SEED is not used, the processor sets the seed for RANDOM_NUMBER to a processor-dependent value."

You could use something like getting the current date ∨ time and using that to calculate anumber to set the seed.

Les

0 Kudos
Robert_van_Amerongen
New Contributor III
2,843 Views
Take a look at Fortran's subroutine RANDOM_SEED. It works togewther withRAMDOM_NUMBER.You may set the argument"put" to a number that is obtained from a call toCPU_TIME, or the component wMillisecond of the API structure SYSTEMTIME (to be obtained with a call to the api routine GetSystemTime) or any other, similar method.

Robert
0 Kudos
Pappu_M_
Beginner
2,843 Views
I am using some subroutine from "Numerical Recipes". It has a static Random seed and I am not sure how it works.
However, I am willing to try if there are inbuilt subroutines that I could use and get a dynamic random number. I have no idea how to go about it though. Please advice.
Thanks.
Pappu Murthy
0 Kudos
Pappu_M_
Beginner
2,843 Views
Not sure where and how i get infor on RANDOM_SEED and RANDOM_NUMBER. Please advice. Thanks.
0 Kudos
TimP
Honored Contributor III
2,843 Views
Your Fortran compiler docs, as well as any textbook on Fortran, will describe these. Generally, these have received more testing and sometimes peer review than NR. Unless you are trying to generate identical sequences with different compilers, you should have little need for the NR.
0 Kudos
anthonyrichards
New Contributor III
2,843 Views
Are you using Intel Visual Fortran? If Yes, then go to Help...Intel Visual Fortran Composer XE 2011...Intel Visual Fortran Composer XE 2011 Help and select the Search and do a search for 'Random' and you will be shown links to all of the intrinsic functions that the compiler makes available for generating random numbers and setting seeds. (n.b. Composer XE 2011 is just the latest version of the Intel Fortran Compiler).
0 Kudos
Pappu_M_
Beginner
2,843 Views
I am using visual fortran the latest version.
I found all about Rand and how to use it. BUt it is still not clear what I need to do (exact statement please), to get totally random sequence of numbers everytime I call. I am not getting the same sequence of numbers everytime i execute the program.
Thanks
0 Kudos
Steven_L_Intel1
Employee
2,843 Views
It is not clear to me what you want.

If you want the same sequence every time you run the program, then either don't call RANDOM_SEED or call it with a specific seed value at the program start, then call RANDOM_NUMBER to get values.

If you want a different sequence every time, call RANDOM_SEED with no arguments at the program start and then use RANDOM_NUMBER to get values.
0 Kudos
Pappu_M_
Beginner
2,843 Views

real(4) ranval

call Random_seed ()

ranval2 = random(0) ! get next random number

ranval3 = random(0)

ranval4 = random(0)

ranval5 = random(1)

print *, ranval2,ranval3,ranval4,ranval5

end


The anpve os ,u cpde amd ot does not work. What am I doing wrong?

0 Kudos
Steven_L_Intel1
Employee
2,843 Views
You want this instead:

[fortran]real(4) ranval(4)
call random_seed
call random_number(ranval)
print *, ranval
end[/fortran]
If you want separate scalar variables, then declare each of the "ranval" variables and call random_number (not "random") for each variable. random_number is a subroutine, not a function.
0 Kudos
Pappu_M_
Beginner
2,843 Views
Your code seems to work, however, here is the problem. Not always it produces different sequences.. Often times I noticed that it produce the exact same numbers.
I tried to copy my screen using the Snipping tool but that is not working. So you just have to believe my words.
I ran like a dozen times the same program and noticed atleast three or 4 times the same 4 numbers (identical to last digit) were produced. This is strange or may be there is a bug.

0 Kudos
Pappu_M_
Beginner
2,843 Views
The above code produces every now and then the same sequence of numbers that to back to back.. I tried about 12 to 20 times and atleast 3 to 4 times i ended up getting the same sequence, mostly back to back and sometimes otherwise as well.
0 Kudos
Steven_L_Intel1
Employee
2,843 Views
When you call random_seed, it uses the time-of-day clock to initialize the seed, down to the second. I have seen it that if I rerun the program very quickly, I can get the same values. This is not likely to be an issue in a real application that runs longer than a second or isn't rerun immediately.

I modified the program to add:

call sleepqq(1000)

before the call to random_seed, and this eliminated the duplicate sequence. This call waits one second.

[plain]C:Projects>r.exe
  0.5473583      0.6576169      0.1448505      0.2380749

C:Projects>r.exe
  0.2513333      0.9547036      0.3125785      0.6219466

C:Projects>r.exe
  0.3072958      0.4003339      6.4170554E-02  0.6977542

C:Projects>r.exe
  0.3632583      0.8459641      0.8157625      0.7735617

C:Projects>r.exe
  0.4192208      0.2915944      0.5673546      0.8493693

C:Projects>r.exe
  0.1231958      0.5886813      0.7350826      0.2332410

C:Projects>r.exe
  0.1791582      3.4311488E-02  0.4866747      0.3090485[/plain]
0 Kudos
Pappu_M_
Beginner
2,843 Views
Thanks. That solved my problem.
0 Kudos
Robert_van_Amerongen
New Contributor III
2,843 Views
Your problem has been solved if you follow the suggestion ofSteve: use RANDOM_SEED without any argument. In that case every new call to this routine causes subsequent calls to RANDOM_NUMBER to havedifferent values.

But that is one part of the story. The other part is this: the Fortran standard tells us that in case RANDOM_SEED is called without any argument, the seed is set to a processor-dependent value. Intel organised this in such a way that different calls to RANDOM_SEED causes that different sequences are obtained. But there is no guarantee that other compilers follow the same way. There is a risk that (any of) these compilersgenerate an equal seed for every call of RANDOM_SEED. So, if you want to write portable software, I suggest you to consider the use of an explicit setting of the put-argument as suggested by some of the contributants of this thread.

Robert
0 Kudos
Andrey_N_Intel
Employee
2,843 Views
Hello,
Pseudo-random number generator is the algortihm for producing the sequence of numbers that look like random and follow some specific stat properties.To start generation of the random numbers you will need to initialize the algorithm with a start value, "seed". If you provide the same seed you will always obain the same sequence of thenumbers. To make sure that each run of your codeis supported with a new sequence of random numbers you will need to initializeyour generator with a new seed.The new seed can be obtained by some "external tools" likea physicalsource of random numbers or system counters.
Please, have a look at the article available at http://software.intel.com/en-us/articles/initializing-Intel-MKL-BRNGs/. While thiscommunication is about initialization of Random Number Generators in Intel Math Kernel Library some of the aspects listed thereareapplicable to random number generators availablein the compiler.
Thanks,
Andrey
0 Kudos
Steven_L_Intel1
Employee
2,843 Views
Andrey's general explanation is correct, but the Fortran standard intrinsics RANDOM_NUMBER and RANDOM_SEED are what I would recommend unless you have specific requirements on the particular RNG used. The Intel Fortran implementation is based on an algorithm by Pierre L'ecuyer as published in "Efficient and Portable Combined Random Number Generators", Communications of the ACM vol 31 num 6 June 1988.
0 Kudos
Reply