|
10 | 10 |
|
11 | 11 | ### random floats
|
12 | 12 |
|
13 |
| -@inline rand(r::AbstractRNG=GLOBAL_RNG) = rand(r, CloseOpen()) |
| 13 | +# CloseOpen(T) is the fallback for an AbstractFloat T |
| 14 | +@inline rand(r::AbstractRNG=GLOBAL_RNG, ::Type{T}=Float64) where {T<:AbstractFloat} = |
| 15 | + rand(r, CloseOpen(T)) |
14 | 16 |
|
15 | 17 | # generic random generation function which can be used by RNG implementors
|
16 | 18 | # it is not defined as a fallback rand method as this could create ambiguities
|
17 |
| -@inline rand_generic(r::AbstractRNG, ::Type{Float64}) = rand(r, CloseOpen()) |
18 | 19 |
|
19 |
| -rand_generic(r::AbstractRNG, ::Type{Float16}) = |
| 20 | +rand_generic(r::AbstractRNG, ::CloseOpen{Float16}) = |
20 | 21 | Float16(reinterpret(Float32,
|
21 | 22 | (rand_ui10_raw(r) % UInt32 << 13) & 0x007fe000 | 0x3f800000) - 1)
|
22 | 23 |
|
23 |
| -rand_generic(r::AbstractRNG, ::Type{Float32}) = |
| 24 | +rand_generic(r::AbstractRNG, ::CloseOpen{Float32}) = |
24 | 25 | reinterpret(Float32, rand_ui23_raw(r) % UInt32 & 0x007fffff | 0x3f800000) - 1
|
25 | 26 |
|
| 27 | +rand_generic(r::AbstractRNG, ::Close1Open2_64) = |
| 28 | + reinterpret(Float64, 0x3ff0000000000000 | rand(r, UInt64) & 0x000fffffffffffff) |
| 29 | + |
| 30 | +rand_generic(r::AbstractRNG, ::CloseOpen_64) = rand(r, Close1Open2()) - 1.0 |
| 31 | + |
26 | 32 | ### random integers
|
27 | 33 |
|
28 | 34 | rand_ui10_raw(r::AbstractRNG) = rand(r, UInt16)
|
@@ -72,6 +78,19 @@ rand( T::Type, d::Integer, dims::Integer...) = rand(T, Dims((d, d
|
72 | 78 | # rand(r, ()) would match both this method and rand(r, dims::Dims)
|
73 | 79 | # moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
|
74 | 80 |
|
| 81 | +#### arrays of floats |
| 82 | + |
| 83 | +rand!(r::AbstractRNG, A::AbstractArray, ::Type{T}) where {T<:AbstractFloat} = |
| 84 | + rand!(r, A, CloseOpen{T}()) |
| 85 | + |
| 86 | +function rand!(r::AbstractRNG, A::AbstractArray, I::FloatInterval) |
| 87 | + for i in eachindex(A) |
| 88 | + @inbounds A[i] = rand(r, I) |
| 89 | + end |
| 90 | + A |
| 91 | +end |
| 92 | + |
| 93 | +rand!(A::AbstractArray, I::FloatInterval) = rand!(GLOBAL_RNG, A, I) |
75 | 94 |
|
76 | 95 | ## Generate random integer within a range
|
77 | 96 |
|
|
0 commit comments