[R-pkg-devel] Problem calling R random number generator from Fortran

Stephen Meyers @rmeyer@2 @end|ng |rom w|@c@edu
Fri Aug 25 17:27:46 CEST 2023


Hello everyone,

I'm updating the 'astrochron' R package, and I am trying to remove the Fortran intrinsic RANDOM_NUMBER from source code, and replace it with R's rand_unif.  I've followed this approach:

https://cran.r-project.org/doc/manuals/R-exts.html#Calling-C-from-Fortran-and-vice-versa

but on complication I get:

/error: implicit declaration of function 'rand_unif' is invalid in C99 [-Werror,-Wimplicit-function-declaration]//
//double F77_SUB(getunif)(void) { return rand_unif(); /

I've created the C wrapper, and then call it from the Fortran code as such:

_C-wrapper:_

    #include <R.h>
    #include <Rmath.h>

    void F77_SUB(getseed)(void) { GetRNGstate(); }
    void F77_SUB(putseed)(void) { PutRNGstate(); }
    double F77_SUB(getunif)(void) { return rand_unif(); }

_Called from the Fortran as:_

          double precision getunif, e

          CALL getseed()

          e = getunif()

          CALL putseed()

Here is link to the full package with the above code implemented:

https://drive.google.com/file/d/1VyfFoWTAtCSYIqB3CZ8i--xL5E4_qz2w/view?usp=sharing

The relevant source is in /src/asm1.8cR.f and src/getunif.c

Many thanks for any advice!

All the best,

  Stephen Meyers

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list