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

External seed for pseudo-random number generator #1012

Open
Flogex opened this issue Dec 8, 2024 · 1 comment
Open

External seed for pseudo-random number generator #1012

Flogex opened this issue Dec 8, 2024 · 1 comment
Assignees
Milestone

Comments

@Flogex
Copy link

Flogex commented Dec 8, 2024

I'm using faker-cxx to generate random data for databases. To test the library and make the generation more deterministic in general, it would be great to be able to provide an explicit seed for the pseudo-random number generator.
Currently, the seed is set with std::random_device:

std::random_device rd;
std::mt19937 gen(rd());

static std::mt19937 pseudoRandomGenerator{std::random_device{}()};

Are you interested in adding this functionality? I'm happy to implement it.


Implementation-wise, I'm considering two alternatives. Let me know if I'm missing something—I'm still new to the code. :)

  1. Providing a seed as parameter of the existing API functions. For example: faker::internet::email(seed=42). The disadvantage is that this would change the signature of all API functions and add an optional parameter. The advantage is that the rest of the code can stay the same (e.g., how RNGs are instantiated).
  2. Using a single thread_local std::mt19937 instead of a static local for every function that needs a random engine, and adding a function to change its seed. This doesn't change the signature of any existing functions.
@cieslarmichal
Copy link
Owner

Hello, very good idea, let's implement it :)
Second solution seems to be better :)
I will assign you to this issue

@cieslarmichal cieslarmichal added this to the v4.0.0 milestone Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants