- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
im new to fortran and im trying to write a test driver program which uses a sub routine norm_rand_number(X) which will return a variable X. The subroutine performs a polar transform using two uniform random numbers U1 AND U2. The code is included, everytime i try to compile i get this subprogram error msg
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL::norm_rand_number
REAL::X
!CALL norm_rand
CALL norm_rand_number(X)
!WRITE RANDOM NUMBERS
WRITE(*,100) 'norm_rand=',X
100 FORMAT (1X,A,F8.5)
END PROGRAM
SUBROUTINE norm_rand_number(X)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::X
REAL::U1,U2,V1,V2,S,Y
!PERFORM POLAR TRANSFORM
DO
CALL SEED(1984)
CALL RANDOM_NUMBER(U1)
CALL SEED(1789)
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
X=SQRT(-2.0*LOG(S)/S)
Y=SQRT(-2.0*LOG(S)/S)
X=V1*X
Y=V2*X
END SUBROUTINE
PROGRAM TEST_normrand
!
!PURPOSE
!PROGRAM TO TEST SUBROUTINE normrand
!
IMPLICIT NONE
!DECLARE LOCAL VARIABLES
REAL::norm_rand_number
REAL::X
!CALL norm_rand
CALL norm_rand_number(X)
!WRITE RANDOM NUMBERS
WRITE(*,100) 'norm_rand=',X
100 FORMAT (1X,A,F8.5)
END PROGRAM
SUBROUTINE norm_rand_number(X)
!PURPOSE:
!TO PRODUCE A NORMALLY DISTRIBUTED RANDOM NUMBER WITH A MEAN OF ZERO AND VARIANCE OF ONE
IMPLICIT NONE
REAL, INTENT(OUT) ::X
REAL::U1,U2,V1,V2,S,Y
!PERFORM POLAR TRANSFORM
DO
CALL SEED(1984)
CALL RANDOM_NUMBER(U1)
CALL SEED(1789)
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
X=SQRT(-2.0*LOG(S)/S)
Y=SQRT(-2.0*LOG(S)/S)
X=V1*X
Y=V2*X
END SUBROUTINE
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Remove this line:
REAL::norm_rand_number
It effectively declares norm_rand_number as a function, but it's not allowed to call a function as if it were a subroutine.
I also question the code in norm_rand_number. You are calling the Fortran intrinsic RANDOM_NUMBER which is fine, but then calling some routine SEED which is not specified. What does SEED do? If you were intending to call the intrinsic RANDOM_SEED, the call is wrong. (It also looks as if you might be trying to force the same random numbers for each call???)
If you wish to "randomize" the start of the random number sequence, simply call RANDOM_SEED with no arguments at the start of the program.
REAL::norm_rand_number
It effectively declares norm_rand_number as a function, but it's not allowed to call a function as if it were a subroutine.
I also question the code in norm_rand_number. You are calling the Fortran intrinsic RANDOM_NUMBER which is fine, but then calling some routine SEED which is not specified. What does SEED do? If you were intending to call the intrinsic RANDOM_SEED, the call is wrong. (It also looks as if you might be trying to force the same random numbers for each call???)
If you wish to "randomize" the start of the random number sequence, simply call RANDOM_SEED with no arguments at the start of the program.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
Remove this line:
REAL::norm_rand_number
It effectively declares norm_rand_number as a function, but it's not allowed to call a function as if it were a subroutine.
I also question the code in norm_rand_number. You are calling the Fortran intrinsic RANDOM_NUMBER which is fine, but then calling some routine SEED which is not specified. What does SEED do? If you were intending to call the intrinsic RANDOM_SEED, the call is wrong. (It also looks as if you might be trying to force the same random numbers for each call???)
If you wish to "randomize" the start of the random number sequence, simply call RANDOM_SEED with no arguments at the start of the program.
REAL::norm_rand_number
It effectively declares norm_rand_number as a function, but it's not allowed to call a function as if it were a subroutine.
I also question the code in norm_rand_number. You are calling the Fortran intrinsic RANDOM_NUMBER which is fine, but then calling some routine SEED which is not specified. What does SEED do? If you were intending to call the intrinsic RANDOM_SEED, the call is wrong. (It also looks as if you might be trying to force the same random numbers for each call???)
If you wish to "randomize" the start of the random number sequence, simply call RANDOM_SEED with no arguments at the start of the program.
man one line? i spent two hours trying to figure this out!! it works fine now and i modified the seed value as suggested 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