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

random number generator

m_p_1
Beginner
366 Views

Hello,

I am testing random generator and what I get in return are hardly random numbers. If I allocate  x with larger size other array elemnts appear to be random but first elemnet is always 0.99*

Can you help what is wrong with the program?

Thanks,

Mark

PROGRAM random_gen

!to test random generator

IMPLICIT NONE

REAL,dimension(1) :: x

INTEGER, ALLOCATABLE :: seed_array(:)
INTEGER :: seed_size,seed_value

INTEGER :: i

CALL RANDOM_SEED (SIZE = seed_size)

ALLOCATE(seed_array(seed_size))


DO i=1,10
CALL extra(seed_value)
PRINT *,seed_value
! seed_array=(/seed_value,seed_value+1/)
seed_array=seed_value
CALL RANDOM_SEED(put=seed_array(1:seed_size))
CALL RANDOM_NUMBER(x)
PRINT *,x
ENDDO

END PROGRAM random_gen

SUBROUTINE extra(val)

IMPLICIT NONE

INTEGER :: val

INTEGER, SAVE :: seed_value
DATA seed_value /0/
seed_value=seed_value+1
val=seed_value
END SUBROUTINE extra

%

random_gen.x
1
0.9999996
2
0.9999993
3
0.9999990
4
0.9999987
5
0.9999984
6
0.9999981
7
0.9999977
8
0.9999974
9
0.9999971
10
0.9999968

0 Kudos
1 Reply
mecej4
Honored Contributor III
366 Views
You called for initialization of the RNG inside the loop. Had you used the same initial seed each time, you would have seen the same real "random" number each time. As it happened, you used a slightly different seed each time, so the real "random" number also turned out to be only slightly different. Moral: seed sparingly and harvest RNGs as often as you need. Unless you need to and know the implications of doing so, do not specify re-seeding.
0 Kudos
Reply