Accessing 3F routine rand in pgf77?

Hi,

I’m using pgf77 5.2-1 x86-64 on a linux AMD opteron system. I’m trying to access the function “rand”, which according to the manual is a “3F subroutine”. Supposedly, these functions are “automatically loaded from the PGI’s Fortran run-time library if referenced by a Fortran program.” However, when try to compile a program (testrand.f) containing rand, I get:

PGFTN-S-0038-Symbol, rand, has not been explicitly declared (testrand.f: 8)

How can I get it to “see” the rand function?

Thanks very much!

Hi dshelly,

How are you calling rand? It should be something like:

% cat testrand.f

        call srand(1)
        x = rand()
        print *, x
        x = rand()
        print *, x
        end
% pgf77 testrand.f
% a.out
  -1.0224130E-27
   3.9686591E+35

Granted, I’m using the 6.1 compiler, but I don’t think this has changed since 5.2.

Hope this helps,
Mat

Hi,

Isn’t rand supposed to generate a number between 0 and 1???

Here’s my test code:


program testrand

implicit none

integer i

do i=1,20
print*,‘rand(0)’,rand(0)
enddo

end


If I remove the “implicit none” then it compiles and gives me the wild numbers like in the above example, as it seems to be accessing an unitialized “variable”. G77 outputs values between 0 and 1 as expected.

Thanks,
David

Hi David,

Ooops, yes your right. I forgot to declare ‘x’ and ‘rand’ as a double precision so they implicitly get defined as a REALs thus changing the value. In your case, the error “rand, has not been explicitly declared” is because you also need to declare ‘rand’ as a double precision. Example:

% cat testrand.f
      program testrand

      implicit none
      integer i
      double precision rand

      do i=1,20
        print *,'rand(0)', rand(0)
      enddo

      end

% pgf77 testrand.f
% a.out
 rand(0)  3.9079850466805510E-014
 rand(0)  9.8539467465030839E-004
 rand(0)  4.1631001594613082E-002
 rand(0)  0.1766426425429160
 rand(0)  0.3646022483906073
 rand(0)  9.1330612112294318E-002
 rand(0)  9.2297647698675434E-002
 rand(0)  0.4872172239468284
 rand(0)  0.5267502797621084
 rand(0)  0.4544334237382444
 rand(0)  0.2331784335639391
 rand(0)  0.8312917879809874
 rand(0)  0.9317314819799343
 rand(0)  0.5680596127871453
 rand(0)  0.5560943324712504
 rand(0)  5.0831914251432408E-002
 rand(0)  0.7670511597301406
 rand(0)  1.8914803475844622E-002
 rand(0)  0.2523597619584059
 rand(0)  0.2981971733712072

Sorry I missed this in your first post.

  • Mat

Ok, thanks - seems to work right now. I guess this arises from rand being double-precision in pgf77 but real in g77? Current test code produces an error in g77. Is there an easy way to make it work right in both?

thanks,
David

For this example, you could declare rand as “REAL” instead of “double precision”. Then when compiling with pgf77 add “-r8” to promote all reals to “REAL*8”.

  • Mat