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

Add lock of random seed #180

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add lock of random seed
  • Loading branch information
dachengx committed Jan 29, 2024
commit d8703a560c448d9ce99286b74f1a95cb376dd331
3 changes: 3 additions & 0 deletions include/NEST/RandomGen.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class RandomGen {
public:
static RandomGen* rndm();
void SetSeed(uint64_t s);
void LockSeed();
void UnlockSeed();
double rand_uniform();
double rand_gauss(double mean, double sigma, bool zero_min = false);
double rand_zero_trunc_gauss(double mean, double sigma);
Expand All @@ -37,6 +39,7 @@ class RandomGen {
// Random number generator object for this class only
// std::ranlux24 rng;
xoroshiro128plus64 rng;
int rng_flag = 0;

static constexpr double xoroshiro128plus64_min =
static_cast<double>(xoroshiro128plus64::min());
Expand Down
12 changes: 12 additions & 0 deletions src/RandomGen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "RandomGen.hh"
#include <stdexcept>

using namespace std;

Expand All @@ -20,10 +21,21 @@ std::uint64_t splitmix64(std::uint64_t z) {
}

void RandomGen::SetSeed(uint64_t s) {
if (rng_flag) {
throw std::runtime_error("You can not change the seed because it is locked.");
}
uint64_t s1 = splitmix64(s);
rng = xoroshiro128plus64(s1, splitmix64(s1));
}

void RandomGen::LockSeed() {
rng_flag = 1;
}

void RandomGen::UnlockSeed() {
rng_flag = 0;
}

double RandomGen::rand_uniform() {
return (static_cast<double>(rng()) - xoroshiro128plus64_min) /
xoroshiro128plus64_minmax;
Expand Down