-
-
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
move Random to stdlib #24874
move Random to stdlib #24874
Conversation
Overall, I think this is the right direction. |
Bump. would be great to get this done. I think we should do only the excision from Base into stdlib for now - and once done, figure out the fate of MersenneTwister separately. |
Sure; but this more or less depends on "distributed" being moved to stdlib first, as it uses |
We could just patch distributed for now to call randstring with the new incantation. I think there is a bit more work there to be done before it can be moved. |
But |
Presumably, distributed just needs an unpredictable path name that won't collide and doesn't need to hook into our "official" random infrastructure. So maybe we could decouple it by just calling C function randslug(n::Int=8)
digits = ['0':'9'; 'a':'z'; 'A':'Z']
s = sprint() do io
while io.size < n
r = ccall(:rand, Cuint, ())
write(io, digits[(r % length(digits)) + 1])
end
end
return s
end It's not very efficient, but it's just being used for random path names. |
Yes I had had the same thought, but got lazy as soon as trying to remember how we do things in C 😁 thanks for having done the work, it helps a lot! |
a4a3785
to
b6f6cbc
Compare
So this should be ready. I kept a referencence in "base/sparse/linalg.jl" to the |
10eb971
to
f42b73d
Compare
As I said, the current state of the PR is to not export by default any rand function when starting the REPL, and I don't even know how to do that. But as this is a merge-conflict magnet, I will merge shortly after CI turns green, and it will be easy to adjust to a different decision if one is taken. |
3834399
to
aef1c9f
Compare
aef1c9f
to
6af87f4
Compare
Help! After many attemps, I'm still unable to find what the error is in Appveyor, while it builds the doc. There is no information shown, and I don't have Windows, so I'm really helpless :( It may be a stupid error, but hard to say when not able to debug... |
6af87f4
to
b00ef11
Compare
Thanks for checking the box! Updated accordingly. Unless requested to change something, I would like to merge by tomorrow if CI turns green. The details can easily be adjusted later, but this tends to accumulate merge conflicts rapidly.
That would be my preference too, or rather |
@@ -0,0 +1,124 @@ | |||
# This file is a part of Julia. License is MIT: https://julialang.org/license | |||
|
|||
# copied verbatim from julia/test/TestHelpers.jl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use what is in test/TestHelpers.jl
? Seems annoying to maintain two exact copies...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I couldn't find the correct path to put in the include
invocation. My short term plan was to move OffsetArrays
from test/TestHelpers.jl
to stdlib/Test
as I think it's generally useful beyond Base
, so it wouldn't be annoying to maintain two copies for a long time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
isdefined(Main, :TestHelpers) || @eval Main include(joinpath(@__DIR__, "../../../test/TestHelpers.jl"))
using Main.TestHelpers.OAs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, this worked! thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the end it didn't work, I had forgotten that the problem arises with on CI (with some invocation of the tests), not with julia stdlib/Random/test/runtests.jl
. I will just do with the duplication for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked for the REPL to stdlib: https://github.com/JuliaLang/julia/pull/25544/files#diff-7b80498051b14c15cd26dd98fcc8e5acR12
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had noticed your solution earlier today, but I don't see how it is different from what Fredrik proposed above ?
base/abstractarray.jl
Outdated
@@ -361,6 +361,8 @@ See also [`checkindex`](@ref). | |||
|
|||
# Examples | |||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (and most other doc examples) don't actually need to use rand
so we should replace those calls with something like fill
instead since random just poisons the example (and even more now with a required using Random
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review! Actually with the new change (rand
is in Base
), using Random
isn't necessary anymore.
base/docs/basedocs.jl
Outdated
@@ -988,6 +988,8 @@ callable with no arguments). The task exits when this function returns. | |||
|
|||
# Examples | |||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand
-> fill
base/essentials.jl
Outdated
@@ -575,6 +575,8 @@ if the index is out of bounds, or has an undefined reference. | |||
|
|||
# Examples | |||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand
-> fill
base/event.jl
Outdated
@@ -118,6 +118,8 @@ If a second argument `val` is provided, it will be passed to the task (via the r | |||
the woken task. | |||
|
|||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand
-> fill
base/permuteddimsarray.jl
Outdated
@@ -28,6 +28,8 @@ See also: [`permutedims`](@ref). | |||
|
|||
# Examples | |||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand
-> fill
base/task.jl
Outdated
@@ -61,6 +61,8 @@ Wrap an expression in a [`Task`](@ref) without executing it, and return the [`Ta | |||
creates a task, and does not run it. | |||
|
|||
```jldoctest | |||
julia> using Random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rand
-> fill
doc/src/stdlib/arrays.md
Outdated
@@ -164,16 +164,16 @@ Base.mapslices | |||
## Combinatorics | |||
|
|||
```@docs | |||
Base.Random.randperm | |||
Base.Random.randperm! | |||
Random.randperm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these move to the random docs instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. As long as Random
is bundled with Base
, it may be useful to have that here as before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up following your suggestion :)
doc/src/stdlib/strings.md
Outdated
@@ -54,7 +54,7 @@ Base.chomp | |||
Base.thisind | |||
Base.nextind | |||
Base.prevind | |||
Base.Random.randstring | |||
Random.randstring |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To random docs?
stdlib/Random/src/Random.jl
Outdated
@@ -359,4 +368,16 @@ true | |||
""" | |||
srand(rng::AbstractRNG, ::Nothing) = srand(rng) | |||
|
|||
|
|||
## deprecations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other stdlibs have an deprecated.jl
file that they include
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK thanks
I think putting random stuff (pun intended) into |
Why is |
I agree it would better together with the sparse functionality, but it's in |
So we should just move it back to |
Yes, we should move |
e452c5f
to
fb2585e
Compare
I updated according to @fredrikekre comments, and kept |
fb2585e
to
960e30b
Compare
This needs to be rebased after #24461. I can do it if you are tired of rebasing this :) |
132d491
to
62da12c
Compare
Thanks so much for the offer! It was an easy one this time so I just did it :) |
3c62d1f
to
84b5fb8
Compare
84b5fb8
to
28c1e6f
Compare
All green, except what seems to be a timeout on AppVeyor, time to merge has finally come 😌 |
Thank you! |
|
||
|
||
## general definitions | ||
|
||
abstract type AbstractRNG end | ||
|
||
defaultRNG() = GLOBAL_RNG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be camel-cased; it violates the style used everywhere else in Base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also if this is just reading from a global const, why not just export the const rather than hiding it behind a function call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be camel-cased;
I didn't really sea this as CamelCased, more as "initials get capitalized"; what would be prefered here? default_rng
? (defaultrng
is un-readable).
why not just export the const rather than hiding it behind a function call
If I remember correctly, it was needed at one point in this PR, because sprand
had GLOBAL_RNG
in its signature, but was in base and couldn't reference a not-yet created global object. Now that SparseArrays
is in stdlib, it should be fine to use GLOBAL_RNG
again but didn't think about it. But I have an impression that for our rand API, it would be better to recommend using a function than a global variable, this gives more freedom evolving the API without breaking change (e.g. defaultRNG
could give a different object depending on the current thread in the future). See also #23205. But this was not the intent in this current PR to push this change, and now that it's not needed anymore, I should probably revert this bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't really sea this as CamelCased, more as "initials get capitalized"
We never capitalize anything in function names, only in type names and consts.
what would be prefered here? default_rng ? (defaultrng is un-readable).
I'd say defaultrng
, though I think just using the const is fine; I don't see how changing the return value of the function would be any more or less breaking than changing the value of a const variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how changing the return value of the function would be any more or less breaking than changing the value of a const variable.
It's less breaking in that if defaultRNG()
returns a different object depending on the thread (in which case GLOBAL_RNG
may be deleted), then it won't be necessary to update rand-related functions (like sprand
) to default to defaultRNG()
instead of GLOBAL_RNG
. But we are not there yet (quite some uncertainty remains regarding the future of GLOBAL_RNG
), and needing to access GLOBAL_RNG
directly should be quite rare anyway, so I will revert this part.
This function is an artifact of an intermediate state of PR #24874, but is not needed anymore.
This function is an artifact of an intermediate state of PR #24874, but is not needed anymore.
This is just an experiment at this point, and I don't know if we want this.
One motivation is that almost no code in base depends on
Random
, so could be stripped out if needed by some installations, another motivation is if this would allow realeasing new versions ofRandom
faster thanBase
(which would allow faster release of improvements which change the generated random streams (which is "breaking")). I'm happy to discuss here alternative plans for those breaking changes, like haveMersenneTwister
without reprocucibility guarranty, and havingMersenneTwister1
frozen to its state as of Julia version 1.0.I had to leave some functions (e.g.
rand
) andAbstractRNG
in Base becausesprand
needs it (I may movesprand
toRandom
eventually, if sparse code stays in base), but also I think it's nice to have at leastrand
,rand!
and maybesrand
available fromBase
directly without importingRandom
.TODOS:
randstring
in "distributed/" once it's moved to "stdlib" (Move Distributed to stdlib #24443)precompile
like other bits moved to stdlib ?