Skip to content
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
merged 10 commits into from
Feb 5, 2025

Conversation

Flogex
Copy link
Contributor

@Flogex Flogex commented Jan 25, 2025

This PR makes it possible to set the seed of the random engine by calling the static method faker::common::SetSeed. Similar to std::mt19937::seed, the method can either take a scalar value or a seed_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 calling faker::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. If SetSeed was not called before, the seed will be obtained from std::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 to RandomGenerator that takes copies an existing random engine. Furthermore, the 64-bit version is now used everywhere because faker::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 using mt19937_64, I'm fine with changing it to using mt19937 everywhere including in date::anytime.

Closes #1012

@cieslarmichal 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
include/faker-cxx/helper.h Outdated Show resolved Hide resolved
src/common/generator.cpp Outdated Show resolved Hide resolved
src/common/generator.cpp Outdated Show resolved Hide resolved
Also fixes a test that can fail on a different platform
@Flogex Flogex requested a review from cieslarmichal February 1, 2025 17:36
include/faker-cxx/helper.h Outdated Show resolved Hide resolved
@cieslarmichal
Copy link
Owner

please fix build

@cieslarmichal cieslarmichal merged commit 2f8d868 into cieslarmichal:main Feb 5, 2025
9 of 11 checks passed
@Flogex Flogex deleted the external-seed branch February 5, 2025 21:32
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External seed for pseudo-random number generator
2 participants