-
-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: use single random engine per thread and provide ability to set seed #1040
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cieslarmichal
changed the title
Use single random engine per thread and provide ability to set seed
feat: use single random engine per thread and provide ability to set seed
Jan 25, 2025
cieslarmichal
requested changes
Jan 27, 2025
Also fixes a test that can fail on a different platform
cieslarmichal
requested changes
Feb 2, 2025
please fix build |
cieslarmichal
approved these changes
Feb 5, 2025
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR makes it possible to set the seed of the random engine by calling the static method
faker::common::SetSeed
. Similar tostd::mt19937::seed
, the method can either take a scalar value or aseed_seq
.Previously,
std::mt19937
random engines where either global static or instantiated on every method call. I changed the code to only use a single random engine per thread, a reference to which can be obtained by callingfaker::common::GetGenerator
. The random engine is thread local to avoid race conditions in case multiple threads try to access it in the future.SetSeed
sets the seed of the thead-local instance corresponding to this thread. IfSetSeed
was not called before, the seed will be obtained fromstd::random_device
. In the current code,std::random_device
is used as well to seed the random engine, probably under the assumption that its entropy is always greater than zero.To be able to replace all instantiations of
std::mt19937
, I added a new constructor toRandomGenerator
that takes copies an existing random engine. Furthermore, the 64-bit version is now used everywhere becausefaker::date::anytime
uses it already and I opted to not change this. I also didn't want to duplicate all members and methods to provide a 32 and a 64 bit version of each. If you have performance concerns when usingmt19937_64
, I'm fine with changing it to usingmt19937
everywhere including indate::anytime
.Closes #1012