Skip to content

Commit 8740ddc

Browse files
committed
seed rng deterministically
1 parent 4153c56 commit 8740ddc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

include/polyscope/utilities.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,37 @@ std::vector<T> gather(const std::vector<T>& input, const std::vector<uint32_t>&
136136

137137

138138
// === Random number generation
139-
extern std::random_device util_random_device;
140-
extern std::mt19937 util_mersenne_twister;
139+
extern std::mt19937 util_mersenne_twister; // deterministically seeded on startup
141140

141+
// Generates a random double in the range [0, 1].
142+
// It is seeded deterministically on startup, so every run of the program will produce the same sequence.
142143
inline double randomUnit() {
143144
std::uniform_real_distribution<double> dist(0., 1.);
144145
return dist(util_mersenne_twister);
145146
}
146147

148+
// Generates a random double in the range [minVal, maxVal].
149+
// It is seeded deterministically on startup, so every run of the program will produce the same sequence.
147150
inline double randomReal(double minVal, double maxVal) {
148151
std::uniform_real_distribution<double> dist(minVal, maxVal);
149152
return dist(util_mersenne_twister);
150153
}
151154

152155
// Generate a random int in the INCLUSIVE range [lower,upper]
156+
// It is seeded deterministically on startup, so every run of the program will produce the same sequence.
153157
inline int randomInt(int lower, int upper) {
154158
std::uniform_int_distribution<int> dist(lower, upper);
155159
return dist(util_mersenne_twister);
156160
}
157161
// Generate a random size_t in the range [0, N)
162+
// It is seeded deterministically on startup, so every run of the program will produce the same sequence.
158163
inline size_t randomIndex(size_t size) {
159164
std::uniform_int_distribution<size_t> dist(0, size - 1);
160165
return dist(util_mersenne_twister);
161166
}
162167

168+
// Generates a random number from a normal (Gaussian) distribution with given mean and standard deviation.
169+
// It is seeded deterministically on startup, so every run of the program will produce the same sequence.
163170
inline double randomNormal(double mean = 0.0, double stddev = 1.0) {
164171
std::normal_distribution<double> dist{mean, stddev};
165172
return dist(util_mersenne_twister);

src/utilities.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
namespace polyscope {
1515

1616
// Globals for random utilities
17-
std::random_device util_random_device;
18-
std::mt19937 util_mersenne_twister(util_random_device());
17+
namespace {
18+
std::seed_seq rng_seed_seq{123456789, 777777777, 987654321};
19+
}
20+
std::mt19937 util_mersenne_twister(rng_seed_seq); // deterministically seeded on startup
1921

2022
std::string guessNiceNameFromPath(std::string fullname) {
2123
size_t startInd = 0;

0 commit comments

Comments
 (0)