Skip to content

RNG Scope? #27

@coatless

Description

@coatless

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.9325398

Under 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.8282708

From 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions