Skip to content

Commit 713986d

Browse files
committed
Random: reorganize some generic fallback methods for floats
1 parent b3121df commit 713986d

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

base/random/RNGs.jl

+2-7
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ RandomDevice
4545

4646
### generation of floats
4747

48-
rand(rng::RandomDevice, ::Close1Open2_64) =
49-
reinterpret(Float64, 0x3ff0000000000000 | rand(rng, UInt64) & 0x000fffffffffffff)
50-
51-
rand(rng::RandomDevice, ::CloseOpen_64) = rand(rng, Close1Open2()) - 1.0
52-
53-
@inline rand(r::RandomDevice, T::BitFloatType) = rand_generic(r, T)
48+
@inline rand(r::RandomDevice, I::FloatInterval) = rand_generic(r, I)
5449

5550

5651
## MersenneTwister
@@ -224,7 +219,7 @@ rand_ui23_raw(r::MersenneTwister) = rand_ui52_raw(r)
224219

225220
@inline rand(r::MersenneTwister, I::FloatInterval_64) = (reserve_1(r); rand_inbounds(r, I))
226221

227-
@inline rand(r::MersenneTwister, T::BitFloatType) = rand_generic(r, T)
222+
@inline rand(r::MersenneTwister, I::FloatInterval) = rand_generic(r, I)
228223

229224
#### integers
230225

base/random/generation.jl

+23-4
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@
1010

1111
### random floats
1212

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))
1416

1517
# generic random generation function which can be used by RNG implementors
1618
# 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())
1819

19-
rand_generic(r::AbstractRNG, ::Type{Float16}) =
20+
rand_generic(r::AbstractRNG, ::CloseOpen{Float16}) =
2021
Float16(reinterpret(Float32,
2122
(rand_ui10_raw(r) % UInt32 << 13) & 0x007fe000 | 0x3f800000) - 1)
2223

23-
rand_generic(r::AbstractRNG, ::Type{Float32}) =
24+
rand_generic(r::AbstractRNG, ::CloseOpen{Float32}) =
2425
reinterpret(Float32, rand_ui23_raw(r) % UInt32 & 0x007fffff | 0x3f800000) - 1
2526

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+
2632
### random integers
2733

2834
rand_ui10_raw(r::AbstractRNG) = rand(r, UInt16)
@@ -72,6 +78,19 @@ rand( T::Type, d::Integer, dims::Integer...) = rand(T, Dims((d, d
7278
# rand(r, ()) would match both this method and rand(r, dims::Dims)
7379
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
7480

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)
7594

7695
## Generate random integer within a range
7796

base/random/random.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const FloatInterval_64 = FloatInterval{Float64}
3131
const CloseOpen_64 = CloseOpen{Float64}
3232
const Close1Open2_64 = Close1Open2{Float64}
3333

34-
CloseOpen() = CloseOpen{Float64}()
35-
Close1Open2() = Close1Open2{Float64}()
34+
CloseOpen( ::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen{T}()
35+
Close1Open2(::Type{T}=Float64) where {T<:AbstractFloat} = Close1Open2{T}()
3636

3737
const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
3838

0 commit comments

Comments
 (0)