Mark Clements
2023-Feb-16 11:58 UTC
[Rd] User-defined RNG with the standalone Rmath library
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>.
