Skip to content

Commit

Permalink
spacefillr v0.3.3: Add support for use as rng in C++ random interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermorganwall committed Apr 2, 2024
1 parent 6f820c7 commit 8b871f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: spacefillr
Type: Package
Title: Space-Filling Random and Quasi-Random Sequences
Version: 0.3.2
Version: 0.3.3
Authors@R: c(person("Tyler", "Morgan-Wall", email = "tylermw@gmail.com",
role = c("aut", "cph", "cre"), comment = c(ORCID = "0000-0002-3131-3814")),
person("Andrew", "Helmer", role = c("ctb", "cph")),
Expand Down
31 changes: 25 additions & 6 deletions inst/include/rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
#include "pcg/pcg_random.hpp"

class random_gen {
public:
random_gen(unsigned int seed) : rng(seed) {}
random_gen() : rng(pcg_extras::seed_seq_from<std::random_device>{}) { }
public:
// Define result_type for compatibility with standard library requirements
using result_type = uint32_t;

random_gen(unsigned int seed) : rng(seed) {}
random_gen() : rng(pcg_extras::seed_seq_from<std::random_device>{}) {}

// The uniform random number generator function
float unif_rand() {
return(std::ldexp(rng(),-32));
return std::ldexp(rng(), -32);
}

uint32_t UniformUInt32(uint32_t b) {
uint32_t threshold = (~b + 1u) % b;
while (true) {
Expand All @@ -20,9 +26,22 @@ class random_gen {
return r % b;
}
}
pcg32 rng;

};
// Call operator to satisfy UniformRandomBitGenerator requirements
result_type operator()() {
return rng();
}

// Static methods to satisfy UniformRandomBitGenerator requirements
static constexpr result_type min() {
return pcg32::min();
}

static constexpr result_type max() {
return pcg32::max();
}

pcg32 rng;
};

#endif

0 comments on commit 8b871f3

Please sign in to comment.