Skip to content

Commit

Permalink
put file handles out of RandomDevice, into global constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Oct 31, 2019
1 parent 8cfb814 commit b626bce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
35 changes: 13 additions & 22 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ if Sys.iswindows()
end
else # !windows
struct RandomDevice <: AbstractRNG
file::IOStream
unlimited::Bool

RandomDevice(; unlimited::Bool=true) =
new(open(unlimited ? "/dev/urandom" : "/dev/random"), unlimited)
RandomDevice(; unlimited::Bool=true) = new(unlimited)
end

rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read( rd.file, sp[])
rand(rd::RandomDevice, sp::SamplerBoolBitInteger) = read(getfile(rd), sp[])

function serialize(s::AbstractSerializer, rd::RandomDevice)
Serialization.serialize_type(s, typeof(rd))
serialize(s, rd.unlimited)
end
function deserialize(s::AbstractSerializer, t::Type{RandomDevice})
unlimited = deserialize(s)
return RandomDevice(unlimited=unlimited)
function getfile(rd::RandomDevice)
devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
if isassigned(devrandom)
devrandom[]
else
devrandom[] = open(rd.unlimited ? "/dev/urandom" : "/dev/random")
end
end

const DEV_RANDOM = Ref{IOStream}()
const DEV_URANDOM = Ref{IOStream}()

end # os-test

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

Expand All @@ -67,7 +68,6 @@ RandomDevice
RandomDevice(::Nothing) = RandomDevice()
seed!(rng::RandomDevice) = rng

const RANDOM_DEVICE = RandomDevice()

## MersenneTwister

Expand Down Expand Up @@ -307,15 +307,6 @@ const THREAD_RNGs = MersenneTwister[]
end
function __init__()
resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object

if !Sys.iswindows()
# open /dev/urandom "in-place" (in RANDOM_DEVICE.file)
RANDOM_DEVICE.file.handle = pointer(RANDOM_DEVICE.file.ios)
systemerror("opening file /dev/urandom",
ccall(:ios_file, Ptr{Cvoid},
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
RANDOM_DEVICE.file.ios, "/dev/urandom", 1, 0, 0, 0) == C_NULL)
end
end


Expand Down
8 changes: 3 additions & 5 deletions stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ using Base.GMP: Limb

using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing

import Base: copymutable, copy, copy!, ==, hash, convert
using Serialization
import Serialization: serialize, deserialize
import Base: rand, randn
import Base: copymutable, copy, copy!, ==, hash, convert,
rand, randn

export rand!, randn!,
randexp, randexp!,
Expand All @@ -29,7 +27,7 @@ export rand!, randn!,
shuffle, shuffle!,
randperm, randperm!,
randcycle, randcycle!,
AbstractRNG, MersenneTwister, RandomDevice, RANDOM_DEVICE
AbstractRNG, MersenneTwister, RandomDevice

## general definitions

Expand Down
4 changes: 0 additions & 4 deletions stdlib/Random/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,3 @@ end
@testset "RNGs broadcast as scalars: T" for T in (MersenneTwister, RandomDevice)
@test length.(rand.(T(), 1:3)) == 1:3
end

@testset "RANDOM_DEVICE" begin
@test rand(Random.RANDOM_DEVICE) isa Float64
end

0 comments on commit b626bce

Please sign in to comment.