-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix: use portable C++ RNG #11627
base: master
Are you sure you want to change the base?
fix: use portable C++ RNG #11627
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
/*! \file | ||
* \brief Psuedo-random number generators class for easier use of C++'s random number generation facilities. | ||
*/ | ||
|
||
#include <random> | ||
#include <limits> | ||
|
||
namespace nix { | ||
|
||
// Inspired by the book "A Tour of C++, Third Edition" (ISBN-10 0136816487) | ||
template<typename T, typename Distribution, typename Engine> | ||
struct RandomNumberGenerator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pseudo random generator, right? We should probably put this in the name so people not use it in the wrong place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is any random number generator really a true random number generator? 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mentioning in the header that this should not be used for cryptographic purpose would be a good start. |
||
{ | ||
public: | ||
using limits = std::numeric_limits<T>; | ||
RandomNumberGenerator(T low = limits::min(), T high = limits::max()) | ||
: engine(std::random_device{}()) | ||
, dist(low, high){}; | ||
RandomNumberGenerator(std::seed_seq seed, T low, T high) | ||
: engine(seed) | ||
, dist(low, high){}; | ||
T operator()() | ||
{ | ||
return dist(engine); | ||
} | ||
void seed(int s) | ||
{ | ||
engine.seed(s); | ||
} | ||
private: | ||
Engine engine; | ||
Distribution dist; | ||
}; | ||
|
||
using RandomIntGenerator = RandomNumberGenerator<int, std::uniform_int_distribution<int>, std::default_random_engine>; | ||
using RandomFloatGenerator = | ||
RandomNumberGenerator<float, std::uniform_real_distribution<float>, std::default_random_engine>; | ||
|
||
} // namespace nix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be part of this PR, but on linux, we can use renameat2 with RENAME_EXCHANGE to make the whole thing atomic without using a temporary directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make a seperate issue for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#11655