From 633ccb360c1e81b14b2978a2f00073a1b456c7a2 Mon Sep 17 00:00:00 2001 From: Robin Deits Date: Tue, 22 Jan 2019 08:49:55 -0500 Subject: [PATCH] srand is now Random.seed! (#601) --- README.md | 3 +++ src/Compat.jl | 39 +++++++++++++++++++++++++++++++++++---- test/runtests.jl | 6 ++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09526830b..4f1f0c4a4 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `atan2` is now a 2-argument method of `atan` ([#27253]). +* `srand` is now `Compat.Random.seed!` ([#28295]) + * `realmin` and `realmax` are now `floatmin` and `floatmax` ([#28302]) * `squeeze` is now `dropdims` ([#28303], [#26660]). @@ -570,5 +572,6 @@ includes this fix. Find the minimum version from there. [#27711]: https://github.com/JuliaLang/julia/issues/27711 [#27828]: https://github.com/JuliaLang/julia/issues/27828 [#27834]: https://github.com/JuliaLang/julia/issues/27834 +[#28295]: https://github.com/JuliaLang/julia/issues/28295 [#28302]: https://github.com/JuliaLang/julia/issues/28302 [#28303]: https://github.com/JuliaLang/julia/issues/28303 diff --git a/src/Compat.jl b/src/Compat.jl index 35e5fb1f2..82122ef25 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -628,10 +628,37 @@ else import SparseArrays end -if VERSION < v"0.7.0-DEV.3406" - const Random = Base.Random -else + +# v"0.7.0-beta.234" introduced Random.gentype (formerly Base.eltype) +# v"0.7.0-beta2.171" deprecated Random.srand in favor of Random.seed! (unexported) +# v"0.7.0-DEV.3406" moved Base.Random to stdlib Random +if VERSION >= v"0.7.0-beta.234" import Random +else + const random_fields = [ + :AbstractRNG, :MersenneTwister, :RandomDevice, :bitrand, :rand, :rand!, + :randcycle, :randexp, :randexp!, :randjump, :randn!, + :randperm, :randstring, :randsubseq, :randsubseq!, :shuffle, + :shuffle! + ] + @eval module Random + if VERSION < v"0.7.0-DEV.3406" + $((:(using Base.Random: $f) for f in random_fields)...) + const seed! = Base.Random.srand + else + $((:(using Random: $f) for f in random_fields)...) + import Random + if VERSION < v"0.7.0-beta2.171" + const seed! = Random.srand + else + using Random: seed! + end + end + + gentype(args...) = eltype(args...) + + export $(random_fields...) + end end if VERSION < v"0.7.0-DEV.3589" @@ -1405,7 +1432,11 @@ if VERSION >= v"0.7.0-DEV.3666" import UUIDs else @eval module UUIDs - import ..Random: uuid1, uuid4, uuid_version, UUID + if VERSION < v"0.7.0-DEV.3406" + import Base.Random: uuid1, uuid4, uuid_version, UUID + else + import Random: uuid1, uuid4, uuid_version, UUID + end export uuid1, uuid4, uuid_version, UUID end end diff --git a/test/runtests.jl b/test/runtests.jl index 9af5fe5e4..7e290d7ef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1500,6 +1500,12 @@ let a = rand(5,5) end end +# 0.7.0-beta2.171 +Random.seed!(1) +rng = MersenneTwister(0) +Random.seed!(rng, 1) +@test rand(rng) ≈ 0.23603334566204692 + # 0.7.0-beta2.169 @test floatmin(Float16) == @eval $(Core.Intrinsics.bitcast(Float16, 0x0400)) @test floatmax(Float32) == @eval $(Core.Intrinsics.bitcast(Float32, 0x7f7fffff))