- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I wonder if anybody can help, i wrote the following random number generator to produce normal random variable truncated between zero and one.
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL ::H
INTEGER ::i,IERROR
iNTEGER ::N=10
DO i=1,N
!CALL norm_rand
CALL norm_rand_number(H)
!WRITE RANDOM NUMBERS
OPEN(20,FILE='GAUS_NORM.TXT',STATUS='REPLACE',ACTION='WRITE',IOSTAT=IERROR)
WRITE(20,100) i,H
100 FORMAT (I4,F8.5)
END DO
END PROGRAM
SUBROUTINE norm_rand_number(H)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::H
REAL::U1,U2,V1,V2,S
REAL, SAVE ::g
LOGICAL, SAVE ::gaus_stored=.false.
do
IF(gaus_stored)THEN
H=g
gaus_stored=.false.
ELSE
!PERFORM POLAR TRANSFORM
DO
!CALL RANDOM_SEED
CALL RANDOM_NUMBER(U1)
CALL RANDOM_NUMBER(U2)
V1=(2.0*U1)-1.0
V2=(2.0*U2)-1.0
S=V1**2+V2**2
IF(S>0.0.AND.S<1.0)EXIT
END DO
S=SQRT((-2.0*LOG(S))/S)
H=V1*S
g=V2*S
gaus_stored=.true.
END IF
IF(H>0.0.AND.H<1.0)EXIT
END DO
END SUBROUTINE
When run in this test bed the random number generator produces a good set of random variables. I then used the random number generator in another program as a subroutine. The program requests random variables at certain times in a counting do loop. However when i print the random variables used to check there 'randomness' the generator produce blocks of the same number. I will get say five numbers which are the same variable before it gives me the next random number which is different. Can anybody suggest anything?
cheers
Andi
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL ::H
INTEGER ::i,IERROR
iNTEGER ::N=10
DO i=1,N
!CALL norm_rand
CALL norm_rand_number(H)
!WRITE RANDOM NUMBERS
OPEN(20,FILE='GAUS_NORM.TXT',STATUS='REPLACE',ACTION='WRITE',IOSTAT=IERROR)
WRITE(20,100) i,H
100 FORMAT (I4,F8.5)
END DO
END PROGRAM
SUBROUTINE norm_rand_number(H)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::H
REAL::U1,U2,V1,V2,S
REAL, SAVE ::g
LOGICAL, SAVE ::gaus_stored=.false.
do
IF(gaus_stored)THEN
H=g
gaus_stored=.false.
ELSE
!PERFORM POLAR TRANSFORM
DO
!CALL RANDOM_SEED
CALL RANDOM_NUMBER(U1)
CALL RANDOM_NUMBER(U2)
V1=(2.0*U1)-1.0
V2=(2.0*U2)-1.0
S=V1**2+V2**2
IF(S>0.0.AND.S<1.0)EXIT
END DO
S=SQRT((-2.0*LOG(S))/S)
H=V1*S
g=V2*S
gaus_stored=.true.
END IF
IF(H>0.0.AND.H<1.0)EXIT
END DO
END SUBROUTINE
When run in this test bed the random number generator produces a good set of random variables. I then used the random number generator in another program as a subroutine. The program requests random variables at certain times in a counting do loop. However when i print the random variables used to check there 'randomness' the generator produce blocks of the same number. I will get say five numbers which are the same variable before it gives me the next random number which is different. Can anybody suggest anything?
cheers
Andi
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - andibrains
I wonder if anybody can help, i wrote the following random number generator to produce normal random variable truncated between zero and one.
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL ::H
INTEGER ::i,IERROR
iNTEGER ::N=10
DO i=1,N
!CALL norm_rand
CALL norm_rand_number(H)
!WRITE RANDOM NUMBERS
OPEN(20,FILE='GAUS_NORM.TXT',STATUS='REPLACE',ACTION='WRITE',IOSTAT=IERROR)
WRITE(20,100) i,H
100 FORMAT (I4,F8.5)
END DO
END PROGRAM
SUBROUTINE norm_rand_number(H)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::H
REAL::U1,U2,V1,V2,S
REAL, SAVE ::g
LOGICAL, SAVE ::gaus_stored=.false.
do
IF(gaus_stored)THEN
H=g
gaus_stored=.false.
ELSE
!PERFORM POLAR TRANSFORM
DO
!CALL RANDOM_SEED
CALL RANDOM_NUMBER(U1)
CALL RANDOM_NUMBER(U2)
V1=(2.0*U1)-1.0
V2=(2.0*U2)-1.0
S=V1**2+V2**2
IF(S>0.0.AND.S<1.0)EXIT
END DO
S=SQRT((-2.0*LOG(S))/S)
H=V1*S
g=V2*S
gaus_stored=.true.
END IF
IF(H>0.0.AND.H<1.0)EXIT
END DO
END SUBROUTINE
When run in this test bed the random number generator produces a good set of random variables. I then used the random number generator in another program as a subroutine. The program requests random variables at certain times in a counting do loop. However when i print the random variables used to check there 'randomness' the generator produce blocks of the same number. I will get say five numbers which are the same variable before it gives me the next random number which is different. Can anybody suggest anything?
cheers
Andi
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL ::H
INTEGER ::i,IERROR
iNTEGER ::N=10
DO i=1,N
!CALL norm_rand
CALL norm_rand_number(H)
!WRITE RANDOM NUMBERS
OPEN(20,FILE='GAUS_NORM.TXT',STATUS='REPLACE',ACTION='WRITE',IOSTAT=IERROR)
WRITE(20,100) i,H
100 FORMAT (I4,F8.5)
END DO
END PROGRAM
SUBROUTINE norm_rand_number(H)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::H
REAL::U1,U2,V1,V2,S
REAL, SAVE ::g
LOGICAL, SAVE ::gaus_stored=.false.
do
IF(gaus_stored)THEN
H=g
gaus_stored=.false.
ELSE
!PERFORM POLAR TRANSFORM
DO
!CALL RANDOM_SEED
CALL RANDOM_NUMBER(U1)
CALL RANDOM_NUMBER(U2)
V1=(2.0*U1)-1.0
V2=(2.0*U2)-1.0
S=V1**2+V2**2
IF(S>0.0.AND.S<1.0)EXIT
END DO
S=SQRT((-2.0*LOG(S))/S)
H=V1*S
g=V2*S
gaus_stored=.true.
END IF
IF(H>0.0.AND.H<1.0)EXIT
END DO
END SUBROUTINE
When run in this test bed the random number generator produces a good set of random variables. I then used the random number generator in another program as a subroutine. The program requests random variables at certain times in a counting do loop. However when i print the random variables used to check there 'randomness' the generator produce blocks of the same number. I will get say five numbers which are the same variable before it gives me the next random number which is different. Can anybody suggest anything?
cheers
Andi
I see that you are using the polar LOG formula to convert uniformly distributed numbers into Gaussian which is correct.
However, you have to start out with a different SEED each time. Are you making sure you aren't initializing the sequence the same way each time?
One reason we use the input SEED is that we want to be able to repeat the same sequence each time for debugging purposes.
I don't see where you are actually inputting the seed. You have the call to RANDOM_SEED commented out.
I would check to see if your uniformly distributed numbers are different each time......
Hope this helps ---- I do this same thing quite often.
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I cant believe i never noticed this myself!! haha i now remebr commenting out for debug, guess its one of those occasions where fresh eyes do wonders!
thanks
thanks

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page