Skip to content

Commit b626bce

Browse files
committed
put file handles out of RandomDevice, into global constants
1 parent 8cfb814 commit b626bce

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

stdlib/Random/src/RNGs.jl

+13-22
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@ if Sys.iswindows()
1919
end
2020
else # !windows
2121
struct RandomDevice <: AbstractRNG
22-
file::IOStream
2322
unlimited::Bool
2423

25-
RandomDevice(; unlimited::Bool=true) =
26-
new(open(unlimited ? "/dev/urandom" : "/dev/random"), unlimited)
24+
RandomDevice(; unlimited::Bool=true) = new(unlimited)
2725
end
2826

29-
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read( rd.file, sp[])
27+
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[])
3028

31-
function serialize(s::AbstractSerializer, rd::RandomDevice)
32-
Serialization.serialize_type(s, typeof(rd))
33-
serialize(s, rd.unlimited)
34-
end
35-
function deserialize(s::AbstractSerializer, t::Type{RandomDevice})
36-
unlimited = deserialize(s)
37-
return RandomDevice(unlimited=unlimited)
29+
function getfile(rd::RandomDevice)
30+
devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
31+
if isassigned(devrandom)
32+
devrandom[]
33+
else
34+
devrandom[] = open(rd.unlimited ? "/dev/urandom" : "/dev/random")
35+
end
3836
end
3937

38+
const DEV_RANDOM = Ref{IOStream}()
39+
const DEV_URANDOM = Ref{IOStream}()
40+
4041
end # os-test
4142

4243
# NOTE: this can't be put within the if-else block above
@@ -48,7 +49,7 @@ for T in (Bool, BitInteger_types...)
4849
A
4950
end
5051
else
51-
@eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(rd.file, A)
52+
@eval rand!(rd::RandomDevice, A::Array{$T}, ::SamplerType{$T}) = read!(getfile(rd), A)
5253
end
5354
end
5455

@@ -67,7 +68,6 @@ RandomDevice
6768
RandomDevice(::Nothing) = RandomDevice()
6869
seed!(rng::RandomDevice) = rng
6970

70-
const RANDOM_DEVICE = RandomDevice()
7171

7272
## MersenneTwister
7373

@@ -307,15 +307,6 @@ const THREAD_RNGs = MersenneTwister[]
307307
end
308308
function __init__()
309309
resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object
310-
311-
if !Sys.iswindows()
312-
# open /dev/urandom "in-place" (in RANDOM_DEVICE.file)
313-
RANDOM_DEVICE.file.handle = pointer(RANDOM_DEVICE.file.ios)
314-
systemerror("opening file /dev/urandom",
315-
ccall(:ios_file, Ptr{Cvoid},
316-
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
317-
RANDOM_DEVICE.file.ios, "/dev/urandom", 1, 0, 0, 0) == C_NULL)
318-
end
319310
end
320311

321312

stdlib/Random/src/Random.jl

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ using Base.GMP: Limb
1616

1717
using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing
1818

19-
import Base: copymutable, copy, copy!, ==, hash, convert
20-
using Serialization
21-
import Serialization: serialize, deserialize
22-
import Base: rand, randn
19+
import Base: copymutable, copy, copy!, ==, hash, convert,
20+
rand, randn
2321

2422
export rand!, randn!,
2523
randexp, randexp!,
@@ -29,7 +27,7 @@ export rand!, randn!,
2927
shuffle, shuffle!,
3028
randperm, randperm!,
3129
randcycle, randcycle!,
32-
AbstractRNG, MersenneTwister, RandomDevice, RANDOM_DEVICE
30+
AbstractRNG, MersenneTwister, RandomDevice
3331

3432
## general definitions
3533

stdlib/Random/test/runtests.jl

-4
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,3 @@ end
778778
@testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
779779
@test length.(rand.(T(), 1:3)) == 1:3
780780
end
781-
782-
@testset "RANDOM_DEVICE" begin
783-
@test rand(Random.RANDOM_DEVICE) isa Float64
784-
end

0 commit comments

Comments
 (0)