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

✨ Repeatable random numbers #1406

Open
cbersch opened this issue Sep 26, 2023 · 0 comments
Open

✨ Repeatable random numbers #1406

cbersch opened this issue Sep 26, 2023 · 0 comments
Labels
feature request An idea for a change, aimed at impoving quality of life
Milestone

Comments

@cbersch
Copy link

cbersch commented Sep 26, 2023

Description

For some of my tests I need repeatable random numbers, e.g. to test serialization of data objects, for which I must compare the resulting string with a reference string, which isn't currently possible.

For this, I would need to configure the seed of the random number generators used in the RegularExpressionGenerator and RandomNumericSequenceGenerator.

I could give it a try, but I'm not sure how to implement this in accordance with the library's design principles.

Suggestion

Maybe one could create a RandomProvider which is injected to the above two generators and provides a Random instance.

However, an instance of this RandomProvider must be delivered through several constructors to the DefaultPrimitiveBuilders constructor.
I think passing this option later at runtime isn't feasible and not reasonable.

public class RandomProvider
{
    public Random Random { get; private set; } = new Random();
    public void SetSeed(int seed) => Random = new Random(seed);
}

public interface IFixture
{
    void SetRandomSeed(int seed);
}

public class Fixture
{
    private readonly RandomProvider random = new RandomProvider();

    public Fixture() 
        : this(new RandomProvider()) { }

    public Fixture(RandomProvider random)
        : this(new DefaultEngineParts(random))
    {
        this.random = random ?? throw new ArgumentNullException(nameof(random));
    }

    public void SetRandomSeed(int seed) => random.SetSeed(seed);
}

// usage
IFixture fixture = new Fixture();
fixture.SetRandomSeed(27);
@aivascu aivascu added feature request An idea for a change, aimed at impoving quality of life triage Something that's being investigated labels Dec 4, 2023
@aivascu aivascu added this to the future milestone Dec 5, 2023
@aivascu aivascu removed the triage Something that's being investigated label Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request An idea for a change, aimed at impoving quality of life
Projects
None yet
Development

No branches or pull requests

2 participants