diff --git a/R/cpp11.R b/R/cpp11.R index 504a97ac..018cbd6a 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -196,6 +196,6 @@ cpp_monty_random_n_multinomial <- function(n_samples, r_size, r_prob, ptr) { .Call(`_monty_cpp_monty_random_n_multinomial`, n_samples, r_size, r_prob, ptr) } -test_xoshiro_run <- function(obj, algorithm) { - .Call(`_monty_test_xoshiro_run`, obj, algorithm) +test_xoshiro_run <- function(ptr) { + .Call(`_monty_test_xoshiro_run`, ptr) } diff --git a/src/cpp11.cpp b/src/cpp11.cpp index 5c3a7f3e..6348c5cb 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -352,10 +352,10 @@ extern "C" SEXP _monty_cpp_monty_random_n_multinomial(SEXP n_samples, SEXP r_siz END_CPP11 } // test_rng.cpp -std::vector test_xoshiro_run(cpp11::sexp obj, std::string algorithm); -extern "C" SEXP _monty_test_xoshiro_run(SEXP obj, SEXP algorithm) { +std::vector test_xoshiro_run(cpp11::sexp ptr); +extern "C" SEXP _monty_test_xoshiro_run(SEXP ptr) { BEGIN_CPP11 - return cpp11::as_sexp(test_xoshiro_run(cpp11::as_cpp>(obj), cpp11::as_cpp>(algorithm))); + return cpp11::as_sexp(test_xoshiro_run(cpp11::as_cpp>(ptr))); END_CPP11 } @@ -410,7 +410,7 @@ static const R_CallMethodDef CallEntries[] = { {"_monty_cpp_monty_rng_set_state", (DL_FUNC) &_monty_cpp_monty_rng_set_state, 2}, {"_monty_cpp_monty_rng_state", (DL_FUNC) &_monty_cpp_monty_rng_state, 1}, {"_monty_monty_rng_alloc", (DL_FUNC) &_monty_monty_rng_alloc, 3}, - {"_monty_test_xoshiro_run", (DL_FUNC) &_monty_test_xoshiro_run, 2}, + {"_monty_test_xoshiro_run", (DL_FUNC) &_monty_test_xoshiro_run, 1}, {NULL, NULL, 0} }; } diff --git a/tests/testthat/test-rng.R b/tests/testthat/test-rng.R index d2365cb7..15fee6f6 100644 --- a/tests/testthat/test-rng.R +++ b/tests/testthat/test-rng.R @@ -333,6 +333,18 @@ test_that("rexp agrees with stats::rexp", { }) + +test_that("can sample from exponential distribution (mean)", { + r1 <- monty_rng_create(seed = 42) + r2 <- monty_rng_create(seed = 42) + expect_equal(monty_random_exponential_mean(0.5, r1), + monty_random_exponential_rate(2, r2)) + expect_equal(monty_random_n_exponential_mean(10, 0.5, r1), + monty_random_n_exponential_rate(10, 2, r2)) +}) + + + test_that("continue stream", { rng1 <- monty_rng_create(seed = 1) rng2 <- monty_rng_create(seed = 1) @@ -1070,6 +1082,9 @@ test_that("negative_binomial_mu follows from negative_binomial_prob", { prob <- 0.3 size <- 20 mu <- size * (1 - prob) / prob + expect_identical( + monty_random_negative_binomial_mu(size, mu, rng1), + monty_random_negative_binomial_prob(size, prob, rng2)) expect_identical( monty_random_n_negative_binomial_mu(100, size, mu, rng1), monty_random_n_negative_binomial_prob(100, size, prob, rng2)) @@ -1125,6 +1140,9 @@ test_that("beta_binomial_prob follows from beta_binomial_ab", { b <- 5 prob <- a / (a + b) rho <- 1 / (a + b + 1) + expect_identical( + monty_random_beta_binomial_prob(size, prob, rho, rng1), + monty_random_beta_binomial_ab(size, a, b, rng2)) expect_identical( monty_random_n_beta_binomial_prob(100, size, prob, rho, rng1), monty_random_n_beta_binomial_ab(100, size, a, b, rng2))