-1

I wanna make rand number without using functions

PROGRAM RTEST
  PARAMETER (N=100)
  IMPLICIT REAL(A-H,O-Z), INTEGER(I-N)
  DIMENSION A(N)
  OPEN(99,FILE='RANDOM.DAT',FORM='FORMATTED')
  IBMO=3149
  SUM=0.0
  DO 5 I=1,N
  IBMO=IBMO*65549
  IF(IBMO) 101, 102, 102
  101 IBMO=IBMO+2147483647+1
  102 RANDOM=0.46566128E-9*IBMO
  A(I)=RANDOM
  WRITE(99,10)I,A(I)
  WRITE(6,10) I,A(I)
   10 FORMAT(5X,I4,3X,F12.7)
  SUM=SUM+A(I)
  AVE1=SUM/FLOAT(N)
   20 FORMAT(12X,F12.7)
   30 FORMAT(5X,I4,3X,F12.7)
  WRITE(6,20) AVE1
  WRITE(99,30)I,AVE1
    5 CONTINUE
  CLOSE(99)
  AVE=SUM/FLOAT(N)
  WRITE(6,*)AVE
  PAUSE
  STOP
  END

but i always got integer overflow or invalid floating point error.....

so what my think was type error

i try to change real >> real*8 and integer >> integer*8

but every try was fail....

what is problem?

enter image description here

albert
  • 8,285
  • 3
  • 19
  • 32
  • Where did you get the "invalid floating point error", try compiling with debug option (like `-g`) so you might get a stack trace. Why not using the standard Fortran functions `RANDOM_INIT`, `RANDOM_SEED` and `RANDOM_NUMBER`? – albert Dec 06 '20 at 11:00
  • i add that case picture! – drink_coffee_joon Dec 06 '20 at 11:06

1 Answers1

0

Your issue is that you need even constants to have a specific type. Here's what you need to do:

PROGRAM RTEST
    USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: int64, real64
    IMPLICIT NONE
    INTEGER, PARAMETER :: N = 100
    REAL(KIND=real64) :: A(N)
    INTEGER(KIND=int64) :: IBMO
    ...
    IBMO = IBMO * 65549_int64
    IBMO = IBMO + 2147483648_int64

and so on. The appended _int64 (after you have imported it from the iso_fortran_env module) tells the compiler to treat this number as a 64-bit integer. Instead of the line use, intrinsic :: iso_fortran_env you can also use the line

INTEGER, PARAMETER :: int64 = selected_int_kind(19)
INTEGER, PARAMETER :: real64 = selected_real_kind(r=300)

(after the IMPLICIT NONE, of course.)

That said, is there a reason you're using such antiquated Fortran syntax?

Any Fortran program that doesn't include the line implicit none is suspicious. Then you're using the syntax

do 5 i = 1, n
...
5 continue

What's wrong with

do i = 1, n
...
end do

And then

if (ibmo) 101, 102, 102

That's syntax I don't even recognise.

francescalus
  • 30,576
  • 16
  • 61
  • 96
chw21
  • 7,970
  • 1
  • 16
  • 31
  • if (ibmo) 101, 102, 102 is if ibmo < 0 go to 101 else if ibmo == 0 goto 102 else if ibmo > 0 go to 102 – drink_coffee_joon Dec 06 '20 at 11:20
  • i learned this fortran grmmar from my old professor... and then in korea we dont have enough data or information to learn fortran so i use just what i learn... – drink_coffee_joon Dec 06 '20 at 11:22
  • It's called an "arithmetic if". It was old fashioned in 1978 and shouldn't have been used since then, and was deleted from the Standard language in 2018 – Ian Bush Dec 06 '20 at 11:55