-
Notifications
You must be signed in to change notification settings - Fork 53
Closed
Labels
Description
Any thoughts on ensuring the correct RNG functionality with the Rmath library?
For instance, if we create a cpp11 function that interfaces with Rf_norm():
cpp11::cpp_source(
code = '
#include "cpp11/doubles.hpp"
#include "R.h"
#include "Rmath.h"
[[cpp11::register]]
double rng_cpp() {
double x = Rf_rnorm(0, 1);
return x;
}
[[cpp11::register]]
double rng_explicit_state_cpp() {
GetRNGstate();
double x = Rf_rnorm(0, 1);
PutRNGstate();
return x;
}', quiet = FALSE)The subsequent running of the versions between C++ and R produce the same result without a seed being re-set.
set.seed(3161611)
rng_cpp()
#> [1] -0.9325398
rnorm(1)
#> [1] -0.9325398Under the second function with an explicit Get/Put state call the desired results are achieved:
set.seed(3161611)
rng_explicit_state_cpp()
#> [1] -0.9325398
rnorm(1)
#> [1] 0.8282708From looking at exported code it seems like there is no wrapping of C++ calls with GetRNGstate()/PutRNGstate(). Perhaps this should be added to the converting documents or addressed during code generation?