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

Suggestion: Add an ability to set PRNG's initial seed #62

Closed
dxball opened this issue Jun 13, 2019 · 1 comment
Closed

Suggestion: Add an ability to set PRNG's initial seed #62

dxball opened this issue Jun 13, 2019 · 1 comment
Assignees

Comments

@dxball
Copy link

dxball commented Jun 13, 2019

Is your feature request related to a problem? Please describe.
Sometimes I need to generate the same result with the same parameter, so the fixed PRNG seed will be one of the ways to do this.

Describe the solution you'd like
Add a constructor to RandomizationBase, and implement in all Randomization class

public abstract class RandomizationBase : IRandomization
{
    protected readonly int seed;
    public RandomizationBase() : this((int)DateTime.Now.Ticks) { }
    public RandomizationBase(int seed) { this. seed = seed; }
}
@giacomelli giacomelli self-assigned this Sep 9, 2022
@giacomelli
Copy link
Owner

I ended up creating two static methods called ResetSeed for BasicRandomization and FastRandomization, because the way that they work right now, as thread-safe, the constructor approach wouldn't work.

Bellow a sample from a unit test showing how to use the ResetSeed method:

[Test]
public void ResetSeed_GetInt_SameResults()
{
   // Resets the seed for the first time.
    FastRandomRandomization.ResetSeed(1);

    var target = new FastRandomRandomization();
    var actual = new int[10];

    for (int i = 0; i < actual.Length; i++)
    {
        actual[i] = target.GetInt(int.MinValue, int.MaxValue);
    }

   // Resets the seed again with the same value, so the pseudorandom values generated by FastRandomRandomization will follow the same order.
   FastRandomRandomization.ResetSeed(1);

    for (int i = 0; i < actual.Length; i++)
    {
        Assert.AreEqual(actual[i], target.GetInt(int.MinValue, int.MaxValue));
    }
}

giacomelli added a commit that referenced this issue Sep 10, 2022
Features
* #62 - Suggestion: Add an ability to set PRNG's initial seed

Bug fixes
* #72 - GeneticAlgorithm.BestChromosome.Fitness decrease over time when using EliteSelection
* #92 - Multiple occurances of same chromosome instance in generation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants