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

SimpleRandom constructor does not set seed value randomly if $seed is null #6

Open
pointybeard opened this issue Oct 8, 2018 · 0 comments

Comments

@pointybeard
Copy link

Problem
Relates to Random/SimpleRandom class around line 40. The code looks like this:

    public function __construct($seed = null)
    {
        if ($seed === null || $seed === 0) {
            $this->seed(mt_rand());
        }
        
        $this->seed($seed);
    }

Notice that if $seed is either null or 0, that mt_rand() is used to populate the seed value with a random value, however, the following line just overrides that with the contents of $seed (either null or 0).

Current Behaviour
Calling new SimpleRandom() (i.e. without providing a seed value) will produce the same seed value everytime resulting in non-random value being generated (for me it's always int(125)).

Expected Behaviour
Calling new SimpleRandom() will produce a random seed value for use internally.

Solution
Modify the constructor to something like this to ensure the result of calling mt_rand() is propagated:

    public function __construct($seed = null)
    {
        if ($seed === null || $seed === 0) {
            $seed = mt_rand();
        }
        
        $this->seed($seed);
    }
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

Successfully merging a pull request may close this issue.

1 participant