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

re-enable the 0-arg MersenneTwister() constructor #21909

Merged
merged 1 commit into from
Sep 3, 2017
Merged

Conversation

rfourquet
Copy link
Member

Cf. #16984 for context, which deprecated MersenneTwister() = MersenneTwister(0).
The next step here is to re-enable this constructor to be equivalent to srand(MersenneTwister(0)).
I think that it's good for predictability that for an RNG type T, T([seed]) produces an RNG object equivalent to srand(T(), [seed])).
Accordingly, srand(RandomDevice()) is also enabled (as a no-op).
I put WIP as I'm not satisfied with my wording in the docstrings.

@rfourquet rfourquet added the randomness Random number generation and the Random stdlib label May 17, 2017
@rfourquet
Copy link
Member Author

Rebased. I will merge in a few days if no objection.

@rfourquet rfourquet changed the title WIP: re-enable the 0-arg MersenneTwister() constructor re-enable the 0-arg MersenneTwister() constructor Aug 26, 2017
The arguments to `srand(rng, ...)` and `typeof(rng)(...)`
should mirror each-other, in particular
`srand(MersenneTwister(0))` and `MersenneTwister()` should
produce an equivalent object.
Also, `srand(::RandomDevice)` has been added for consistency,
which could be useful in generic code.
"""
RandomDevice

RandomDevice(::Void) = RandomDevice()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not. For MersenneTwister, I introduced it for convenience (but still not necessary), and I thought it could be a nice API (not yet documented): when you want to initialize your RNG, you can default your seed to nothing to have the behavior of non-reproducibility. WIthout nothing, you have to handle the 2 cases:

seed = Nullable{Int}()
...
rng = isnull(seed) ? MersenneTwister() : MersenneTwister(seed)
# vs:
rng = MersenneTwister(get(seed, nothing))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't you splat an empty tuple? we don't often use nothing as an input flag, other than unset keywords

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't you splat an empty tuple? we don't often use nothing as an input flag, other than unset keywords

I guess splatting a tuple would work... but it seams more natural to me to use nothing, and your mention of keyword is helpful: it's easy to have an API like do_computation(...; seed=nothing) and feed the RNG directly with seed without having to splat a tuple. I remember few times having found inconvenient to not be able to give a default value to a seed which could mean "seed randomly". A couple of relevant (kind of) examples which receive a seed as parameter which could benefit from allowing nothing: https://github.com/denizyuret/Knet.jl/pull/105/files and https://github.com/JuliaGraphs/LightGraphs.jl/pull/579/files

@rfourquet rfourquet merged commit f4edae1 into master Sep 3, 2017
@rfourquet rfourquet deleted the rf/MT-ctor branch September 3, 2017 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants