[Rd] User-defined RNG with the standalone Rmath library

Mark Clements m@rk@c|ement@ @end|ng |rom k|@@e
Thu Feb 16 12:58:18 CET 2023


I have two questions about using a user-defined random number generator (RNG) with the standalone Rmath library. The default RNG with the standalone Rmath library is the Marsaglia-multicarry generator, which has poor properties. The "R Installation and Administration" manual, in the section "The standalone Rmath library", states that:

```
A little care is needed to use the random-number routines. You will need to supply the uniform random number generator

    double unif_rand(void)

or use the one supplied (and with a shared library or DLL you may have to use the one supplied, which is the Marsaglia-multicarry with an entry point

    set_seed(unsigned int, unsigned int)

to set its seeds).
```

I interpret this to mean that we cannot use a user-defined random number generator with "a shared library or DLL", which suggests using static linking.

Question: are there any good examples of re-defining unif_rand() with the standalone Rmath library?

As an example, we could set up a file test.c:

  #define MATHLIB_STANDALONE
  #include <Rmath.h>
  #include <stdio.h>
  double unif_rand(void) { return 0.5; }
  int main() {
    printf("%f\n", unif_rand());
    printf("%f\n", runif(0, 1));
    return 0;
  }

If we compile using -static, then we get the correct result (two values of 0.5):

  gcc -static -o test test.c -lRmath -lm
  ./test
: 0.500000
: 0.500000

Question: does this code work for other users?

Sincerely, Mark.


När du skickar e-post till Karolinska Institutet (KI) innebär detta att KI kommer att behandla dina personuppgifter. Här finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.


Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI’s processing of personal data here<https://ki.se/en/staff/data-protection-policy>.


More information about the R-devel mailing list