@@ -19,24 +19,25 @@ if Sys.iswindows()
19
19
end
20
20
else # !windows
21
21
struct RandomDevice <: AbstractRNG
22
- file:: IOStream
23
22
unlimited:: Bool
24
23
25
- RandomDevice (; unlimited:: Bool = true ) =
26
- new (open (unlimited ? " /dev/urandom" : " /dev/random" ), unlimited)
24
+ RandomDevice (; unlimited:: Bool = true ) = new (unlimited)
27
25
end
28
26
29
- rand (rd:: RandomDevice , sp:: SamplerBoolBitInteger ) = read ( rd . file , sp[])
27
+ rand (rd:: RandomDevice , sp:: SamplerBoolBitInteger ) = read (getfile (rd) , sp[])
30
28
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
38
36
end
39
37
38
+ const DEV_RANDOM = Ref {IOStream} ()
39
+ const DEV_URANDOM = Ref {IOStream} ()
40
+
40
41
end # os-test
41
42
42
43
# NOTE: this can't be put within the if-else block above
@@ -48,7 +49,7 @@ for T in (Bool, BitInteger_types...)
48
49
A
49
50
end
50
51
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)
52
53
end
53
54
end
54
55
@@ -67,7 +68,6 @@ RandomDevice
67
68
RandomDevice (:: Nothing ) = RandomDevice ()
68
69
seed! (rng:: RandomDevice ) = rng
69
70
70
- const RANDOM_DEVICE = RandomDevice ()
71
71
72
72
# # MersenneTwister
73
73
@@ -307,15 +307,6 @@ const THREAD_RNGs = MersenneTwister[]
307
307
end
308
308
function __init__ ()
309
309
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
319
310
end
320
311
321
312
0 commit comments