Random numbers continue to do the same, despite random seed use

I have the following small piece of code:

REAL(8) :: x INTEGER :: i call system_clock(i) WRITE(*,*) 'cpu time', i CALL random_seed(i) CALL random_number(x) WRITE(*,*) 'uniform RandVar', x 

CPU time is working fine, but every time I run it, I get the same uniform RandVar number = 0.99755959009261719 , almost like random_number uses the same default value over and over again and ignores random seed.

What am I doing wrong?

+7
random fortran
source share
1 answer

You can use the same seed: it depends on the processor. The reason for this is because your random_seed call random_seed not set the seed.

With reference

 CALL random_seed(i) 

argument i not a ( intent(in) ) seed, but ( intent(out) ) is the size of the seed used by the processor. This call is like

 CALL random_seed(SIZE=i) ! SIZE is the first dummy argument 

To set a seed, you need to explicitly associate a dummy argument with the PUT argument: call random_seed(put=seed) . Here the seed is an array of rank 1 of size at least n , where n - again depends on the processor - this is the size specified by call random_seed(size=n) . From your call i , this value is executed.

Detailed information is given in 13.7.136 F2008.

A general way to generate a generator is:

 integer, allocatable :: seed(:) integer size call random_seed(size=size) allocate(seed(size)) ! set seed(:) somehow call random_seed(put=seed) 

Setting seed not a simple process. I am not considering how to do this, but the details can be found in the answers to this other question .

The use of srand() mentioned in the comments is non-standard.

+3
source share

All Articles