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

make MersenneTwister() randomly seeded #16984

Merged
merged 3 commits into from
Apr 6, 2017
Merged

make MersenneTwister() randomly seeded #16984

merged 3 commits into from
Apr 6, 2017

Conversation

rfourquet
Copy link
Member

@rfourquet rfourquet commented Jun 17, 2016

I have been meaning to propose this change for a while: the default constructor becomes MersenneTwister() = srand(MersenneTwister(0)) instead of MersenneTwister() = MersenneTwister(0). In other words, rand(MersenneTwister()) will yield changing values. I couldn't find a discussion on this topic, and it was like that since the "beginning" (68049d4).

I guess this comes from Matlab behavior; an example of a language which made the other choice is python, where an object random.Random() is seeded randomly by default. In Julia the global RNG is randomly seeded, but explicit instances of MersenneTwister are not which appear a bit odd to me.

In the work I do, I never rarely want the repetitive behavior of MersenneTwister(0), so I always call r = srand(MersenneTwister()) to get a new RNG r. Not a big deal, but IMHO if one needs a specific seed, one should be stating that explicitly with MersenneTwister(0). Furthermore, as this RNG stores the seed it was initialized with, it's pretty easy to re-generate a particular sequence if needed.

Will update the breaking section of NEWS.md if there is support for this change.

@rfourquet rfourquet added randomness Random number generation and the Random stdlib needs decision A decision on this change is needed labels Jun 17, 2016
@rfourquet
Copy link
Member Author

Bump! Would be happy to hear some opinions on this change.

@vchuravy vchuravy requested a review from simonbyrne March 29, 2017 13:08
@andreasnoack
Copy link
Member

I think we should make this change.

@StefanKarpinski
Copy link
Member

💯 Yes, I would be very badly surprised if I called this and got an implicit seed of zero. I even think this is worth violating the feature freeze since this is definitely not how this should work.

@KristofferC
Copy link
Member

I agree with the change but looking through a few of the pages in https://github.com/search?utf8=%E2%9C%93&q=MersenneTwister%28%29+language%3AJulia&type=Code, this will be quite breaking.

@StefanKarpinski
Copy link
Member

Most of the calls have a seed argument – how about deprecating the no-argument constructor for now so that we can reintroduce it with random seed later?

@ViralBShah
Copy link
Member

I feel that we should just not have a no-argument version at all, and always require an implicit seed. The only exception should be the rand() that uses the GLOBAL_RNG, for convenience.

@rfourquet
Copy link
Member Author

So there is consensus for at least removing the current behavior :)

and always require an implicit seed.

The problem is when one wants a random seed. The current way srand(MersenneTwister()) is OK but it seems so natural to have the default constructor give a randomly seeded RNG... and I don't see why the GLOBAL_RNG would have to be an exception.

Would the way forward be to deprecate MersenneTwister() now, and to introduce the new behaviour (if it is decided upon) in the subsequent release?

@ViralBShah
Copy link
Member

I could be ok with having a convenience method for the random seed case too. If we are going to change the behaviour this release, we should do it now for 0.6, so that 1.0 can have the final new behaviour.

@rfourquet
Copy link
Member Author

So I don't know the deprecation policy for this case: if we deprecate now MersenneTwister() for 0.6 and re-enable it (with different behavior) for 1.0, then we don't follow the rule in deprecated.jl "A function deprecated in a release will be removed in the next one." Can we just instead make this call directly an error now (instead of a deprecation)?

@tkelman
Copy link
Contributor

tkelman commented Mar 30, 2017

Can we just instead make this call directly an error now (instead of a deprecation)?

No, that would be rather breaking, and we're in feature freeze so technically it's too late to be adding new deprecations. Exceptions could be made, but we better be sure about them.

Not all deprecations have to be deleted later, some could be for behavior changes.

@rfourquet
Copy link
Member Author

I rebased and made the deprecation. I am not competent to decide whether this can be merged for 0.6 (just noting that it would be great to have the new behavior for 1.0).

@ViralBShah ViralBShah added this to the 0.6.0 milestone Mar 31, 2017
@rfourquet rfourquet force-pushed the rf/MT-random-seed branch from c6f1d81 to a07cc37 Compare April 1, 2017 01:39
@tkelman
Copy link
Contributor

tkelman commented Apr 1, 2017

It's pretty late to be adding new deprecations for 0.6 since we've already had an alpha and just tagged beta, but since this one is so trivial I'd be okay with it if you're willing to go make PR's that fix it. Looks like it's only used in 9 registered packages: https://gist.github.com/44be8ad4da005a3db07108dc6520d6f2

@rfourquet
Copy link
Member Author

Thanks @tkelman your list of packages to patch was super helpful.

@ViralBShah
Copy link
Member

These all seem like reasonably widely used and maintained packages.

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Apr 6, 2017

The only package PR that hasn't been merged is the DSGE one, so we can go ahead with this as soon as CI passes.

@ararslan ararslan merged commit 5bcfd2c into master Apr 6, 2017
@ararslan ararslan deleted the rf/MT-random-seed branch April 6, 2017 18:14
@rfourquet
Copy link
Member Author

An old discussion on this topic, which eventually agrees with the change here: #67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants