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

Why "Call Random_Number" returns same numbers when I run multiple simulations simultaneously?

Maadi
Beginner
2,494 Views
I am running Monte Carlo simulation and I have to run 1 million replications.
To save time I have to run simulations simultaneously and using "Call Random_seed " to change my random seed and using command "Call random_number()" to generate random numbers.

I have noticed that when I run 20 simulations simultaneously I get some repeated results. This problem is because same random numbers are generated.

How I can resolve this problem?
I am using IVF11 with windows.

I appreciate your help

0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,494 Views
Is this parallel all within the same program run once, or in multiple copies of the program run in parallel? If the same program run once, just call RANDOM_SEED once. If it is programs run in parallel, you will need to construct your own seed that differs from run to run. It could use the process ID in combination with the value from SYSTEM_CLOCK to create a seed. The process ID can be obtained by a call to the Windows API routine GetCurrentProcessId (defined in module KERNEL32).

View solution in original post

0 Kudos
5 Replies
JohnNichols
Valued Contributor III
2,494 Views

In about 1986 Scientific American published a column by the Math Guru, whose name I forget, but it required a random number generator to generate genetic code elements. The original code was published in BASIC, and I used MicroSOFT Basic at the time to run the program.It worked a real treat and I enjoyed playing with it on a COMPAQ portable, with two floopy drives and a 100 mm green screen.

I translated the code to Fortran and could never get it to run, because of the exact problem you describe.


If you can inlcude the VB random number generator that might work.

Just a thought

JMN

0 Kudos
JVanB
Valued Contributor II
2,494 Views
CALL RANDOM_SEED() with no arguments likely uses the time of day to the nearest second or centisecond or so, and when called back-to-back can return the same seed. You can try to fix this by a call to RANDOM_SEED(SIZE=array_size) to see how big an array it uses, then ALLOCATE(SEED(array_size)) to allocate the default integer array SEED to the correct size and then you can use the sequence:

CALL RANDOM_SEED()
CALL RANDOM_SEED(GET=SEED)
default_integer = QueryPerformanceCounter(lpPerformanceCount)
SEED(1) = lpPerformanceCount
CALL RANDOM_SEED(PUT=SEED)

to first get a seed that is mostly random, but only changes every second or so, then change its first element to a number that changes every clock cycle or so, and put it back in. Not guaranteed to get you a different seed every time, but not as bad as calling RANDOM_SEED() back-to-back without modification.
0 Kudos
Steven_L_Intel1
Employee
2,494 Views
The resolution of the value RANDOM_SEED uses when no arguments are passed seems to be somewhat coarse, in seconds perhaps. But calling RANDOM_SEED in parallel isn't going to help you any. Call it once at the beginning of the program and you should be ok.
0 Kudos
Maadi
Beginner
2,494 Views
The problem is I have to run simulations in parallel.
Let's say I need to generate new random seed 20 time in a second. In the beginning of every replication I am calling Randdom-seed and after it is done for new replication I am calling that again. But all my 20 replications which are running simultaneously are running independently in parallel.
0 Kudos
Steven_L_Intel1
Employee
2,495 Views
Is this parallel all within the same program run once, or in multiple copies of the program run in parallel? If the same program run once, just call RANDOM_SEED once. If it is programs run in parallel, you will need to construct your own seed that differs from run to run. It could use the process ID in combination with the value from SYSTEM_CLOCK to create a seed. The process ID can be obtained by a call to the Windows API routine GetCurrentProcessId (defined in module KERNEL32).
0 Kudos
Reply