random number generator

random number generator

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

2 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

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.

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui