Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use R_unif_index() instead of unif_rand() and move RNGScope #24

Merged
merged 3 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# dqrng (unreleased)

* Add R side support for selecting multiple streams for parallel usage.
* Implement `long_jump()` for Xo(ro)shiro as alternative to `jump()` providing fewer streams with longer period.
* Implement `long_jump()` for Xo(ro)shiro as alternative to `jump()`
providing fewer streams with longer period.
* Handle R's RNG scope properly during initialisation.
* New functions `dqsample` and `dqsample.int` using an unbiased sampling algorithm
* New functions `dqsample` and `dqsample.int` using an unbiased sampling
algorithm.
* Use `R_unif_index()` instead of `unif_rand()` to retrieve random data
from R's RNG in `generateSeedVectors()`.

# dqrng 0.1.1

Expand All @@ -15,7 +19,7 @@
## Breaking changes

* An integer vector instead of a single `int` is used for seeding (Aaron Lun in [#10](https://github.com/daqana/dqrng/pull/10))
* Single integer seeds lead to a different RNG state than before.
* Single integer seeds lead to a different RNG state than before.
* `dqrng::dqset_seed()` expects a `Rcpp::IntegerVector` instead of an `int`
* Support for Mersenne-Twister has been removed, Xoroshiro128+ is now the default.

Expand Down
13 changes: 8 additions & 5 deletions inst/include/R_randgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@

namespace dqrng {

/* Create a uint32_t or int seed using R's PRNGs.
* The uint32_t seed is more convnient from use within C++,
/* Create a uint32_t or int seed using R's PRNGs.
* The uint32_t seed is more convenient from use within C++,
* the int seed is more convenient if it needs to be passed back to R.
* Since R's PRNG is used, calling one of these functions has to be shielded
* with calls to GetRNGState and PutRNGState, e.g. by using Rcpp::RNGScope.
* This is done automatically when using the Rcpp::export attribute.
*/

inline uint32_t R_random_u32 () {
constexpr double upper_limit=4294967296;
double val=R::unif_rand() * upper_limit;
double val = R_unif_index(upper_limit);
if (val >= upper_limit) { val=0; } // Absolutely avoid overflow.
return static_cast<uint32_t>(val);
}

inline int R_random_int () {
const uint32_t sampled=R_random_u32();
constexpr uint32_t max_int=2147483647; // See .Machine$integer.max.
if (sampled <= max_int) {
if (sampled <= max_int) {
return static_cast<int>(sampled);
}

// Effectively reverse of the int->uint32_t cast.
constexpr uint32_t max_uint=-1;
return -static_cast<int>(max_uint - sampled) - 1;
Expand Down