-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
define show(::MersenneTwister) #25129
Conversation
Can't we just add a method such that |
It works in that it creates an RNG with the same seed, but not equal (unless the printed RNG has not generated any values since its seeding), so this is not a way to construct back the RNG. There may be a way by counting how many values have been generated, and using julia> m = MersenneTwister(0)
MersenneTwister(seed=0x0, advance=0)
julia> rand(m)
MersenneTwister(seed=0x0, advance=1)
julia> ans == MersenneTwister(seed=0x0, advance=1)
true But this would require #16906 to jump by arbitrary many steps, and I'm not sure it's worth the trouble (I don't see use case). But maybe printing |
I forgot to say: this kind of depends on #23546; indeed, feeding back the seed in hexadecimal to an RNG works as long as it fits a |
I prefer the |
If advance counting works, then I agree with Stefan that |
|
I don't care for the |
I didn't investigate the overhead of counting, but it should be very low, like incrementeing an integer every few hundreds generated The trouble is also that the PR implementing the functionality to advance effectively hasn't be updated for a while and there is now way I can do it by the 0.7 feature freeze timeframe. (It should interact very little with other functionalities though, so it would seem safe to introduce later between 0.7 and 1.0. ) So if we want this for 0.7, I like Jameson's suggestion with |
The advance functionality doesn't need to work now. That will just lead to a situation where we print an RNG as |
I thought a little bit more about this, and the "advance" functionality still sounds tricky but doable. Let's start with a small example, and recall that MT has a cache of
In both cases, julia> m = MersenneTwister(3); rand(m, 2^10); m
MersenneTwister(0x3, 0x0xd4e8b116a747b4fc7dbf67862e68478a) Isn't it too much? At least it's not worse than the current printing, and we can have a note in the doc about this mysterious 2nd number. |
I'm not actually super concerned about being able to print an output syntax that reproduce the same exact RNG state – being able to de/serialize RNG objects would accomplish the same thing. How about some output format that's clearly not an input syntax then, like |
737546a
to
da45200
Compare
Small update: in addition to the
I would be fine with something like that, but what is 1024 here? |
The |
I like |
So we seem to be converging! julia> srand(123)
MersenneTwister(0x7b, (0, 0, 0)) # where is my 123 ?? But on the other hand, the default seeding uses 4 random julia> srand()
MersenneTwister(282547262182945653700010255641608774292, (0, 0, 0))
julia> srand() # versus
MersenneTwister(0xd4909f9036b5e5b14cc424a1f798e294, (0, 0, 0)) it's just a bunch of bits used to initialize the states, and I believe hexadecimal conveys this idea. Also, it is consistent with the fact that only positive integers are allowed as the the seed. So the DIWM could be printing in hex when the seed is too big to fit an |
I would just do the DWIM thing. Seems like a better experience. |
I implemented the DWIM. Also, we don't really need two julia> m = MersenneTwister()
MersenneTwister(0xc5801071048f3cd7a2982d00e152c3ca, (0, -1, 0))
julia> rand(m); m
MersenneTwister(0xc5801071048f3cd7a2982d00e152c3ca, (382, 0, 1))
julia> [rand(m) for i in 1:381]; m
MersenneTwister(0xc5801071048f3cd7a2982d00e152c3ca, (382, 0, 382))
julia> rand(m); m
MersenneTwister(0xc5801071048f3cd7a2982d00e152c3ca, (764, 382, 1))
julia> srand(m, 123)
MersenneTwister(123, (0, -1, 0))
julia> randjump(m, 2)
2-element Array{MersenneTwister,1}:
MersenneTwister(123, (764, 382, 1))
MersenneTwister(123, (7766279631452241920, 0, -1, 0)) The last example shows a 4th number, the size of the jump. It would also be meaningful to not print it but to add it to the first 2 numbers of the triple, but it becomes harder to read: I think I'm pretty satisfied with this current version :) |
So I ended up rebasing #16906 and implemented a |
52eaf0c
to
da129b8
Compare
I rebased this, with the complication that we now have two caches in I tried a compromise where the integer corresponding to the number julia> m = MersenneTwister(123)
MersenneTwister(123, (0, 0, 0))
julia> rand(m); m
MersenneTwister(123, (1002, 0, 1))
julia> rand(m, Int64); m
MersenneTwister(123, (2002, 0, 255, 1002, 0, 1, 1))
julia> m = Future.randjump(MersenneTwister(123), 10^9)
MersenneTwister(123, (2000000000, 0, 0, 0))
julia> rand(m, Int128); m
MersenneTwister(123, (2000000000, 2002, 1000, 254, 0, 0, 0, 2)) For the first example, we could also simply show Feedback welcome! |
Bump. Would be nice to find a solution here, to remove the fear of calling |
Can/do we provide the corresponding constructors such that
etc.? |
I have them available locally, and I think it would be good to provide them yes. Just waiting for input on whether we go this route (to finalize the code), and whether I open a separate PR for the constructors. |
Ok, so I like it if this helps :-) |
da129b8
to
63fa835
Compare
Ok, I finally finalized the code implementing the constructors matching Feed-back welcome! If no discussion or objection come, I will merge soon... |
1439840
to
1e9f32f
Compare
fc43c51
to
7a19359
Compare
E.g. ``` julia> m = MersenneTwister(0); rand(m); m MersenneTwister(0, (0, 1002, 0, 1)) julia> m == MersenneTwister(0, (0, 1002, 0, 1)) true ```
I have been meaning to do this for a long time. I would say it's mostly useful because
srand
returns the RNG, so it happens at the REPL to be show aMersenneTwister
, with loads of useless information:Alternative 2 is a bit ugly, but without the dots, it mistakenly makes you think you can get an equal RNG with
MersenneTwister(0xd4e8b116a747b4fc7dbf67862e68478a)
, (same problem with alternative 3), and with the dots, it makes you think there is a multi-arg constructor, which is not the case.I like to print the seed as an unsigned number, as it's just a field of bits, and the unsigned can be fed back as a seed later.
Suggestions welcome!