@@ -34,6 +34,24 @@ export srand,
3434
3535abstract type AbstractRNG end
3636
37+ """
38+ Random.gentype(x)
39+
40+ Determine the type of the elements generated by calling `rand([rng], x)`.
41+ When `x` is not a type, `gentype(x)` defaults to `eltype(x)`,
42+ and for types, `gentype(x)` defaults to `x`.
43+
44+ # Examples
45+ ```jldoctest
46+ julia> gentype(1:10)
47+ Int64
48+
49+ julia> gentype(Float64)
50+ Float64
51+ ```
52+ """
53+ gentype (x) = eltype (x)
54+ gentype (:: Type{X} ) where {X} = X
3755
3856# ## integers
3957
@@ -72,7 +90,7 @@ for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw,
7290 end
7391end
7492
75- Base . eltype (:: Type{<: UniformBits{T} } ) where {T} = T
93+ gentype (:: UniformBits{T} ) where {T} = T
7694
7795# ## floats
7896
@@ -88,15 +106,15 @@ const CloseOpen12_64 = CloseOpen12{Float64}
88106CloseOpen01 (:: Type{T} = Float64) where {T<: AbstractFloat } = CloseOpen01 {T} ()
89107CloseOpen12 (:: Type{T} = Float64) where {T<: AbstractFloat } = CloseOpen12 {T} ()
90108
91- Base . eltype (:: Type{<: FloatInterval{T} } ) where {T<: AbstractFloat } = T
109+ gentype (:: FloatInterval{T} ) where {T<: AbstractFloat } = T
92110
93111const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
94112
95113# ## Sampler
96114
97115abstract type Sampler{E} end
98116
99- Base . eltype (:: Type{<: Sampler{E} } ) where {E} = E
117+ gentype (:: Sampler{E} ) where {E} = E
100118
101119# temporarily for BaseBenchmarks
102120RangeGenerator (x) = Sampler (GLOBAL_RNG, x)
@@ -133,7 +151,7 @@ struct SamplerTrivial{T,E} <: Sampler{E}
133151 self:: T
134152end
135153
136- SamplerTrivial (x:: T ) where {T} = SamplerTrivial {T,eltype(T )} (x)
154+ SamplerTrivial (x:: T ) where {T} = SamplerTrivial {T,gentype(x )} (x)
137155
138156Sampler (:: AbstractRNG , x, :: Repetition ) = SamplerTrivial (x)
139157
@@ -145,7 +163,7 @@ struct SamplerSimple{T,S,E} <: Sampler{E}
145163 data:: S
146164end
147165
148- SamplerSimple (x:: T , data:: S ) where {T,S} = SamplerSimple {T,S,eltype(T )} (x, data)
166+ SamplerSimple (x:: T , data:: S ) where {T,S} = SamplerSimple {T,S,gentype(x )} (x, data)
149167
150168Base. getindex (sp:: SamplerSimple ) = sp. self
151169
223241rand (r:: AbstractRNG , dims:: Integer... ) = rand (r, Float64, Dims (dims))
224242rand ( dims:: Integer... ) = rand (Float64, Dims (dims))
225243
226- rand (r:: AbstractRNG , X, dims:: Dims ) = rand! (r, Array {eltype (X)} (undef, dims), X)
244+ rand (r:: AbstractRNG , X, dims:: Dims ) = rand! (r, Array {gentype (X)} (undef, dims), X)
227245rand ( X, dims:: Dims ) = rand (GLOBAL_RNG, X, dims)
228246
229247rand (r:: AbstractRNG , X, d:: Integer , dims:: Integer... ) = rand (r, X, Dims ((d, dims... )))
@@ -232,7 +250,7 @@ rand( X, d::Integer, dims::Integer...) = rand(X, Dims((d, dims...
232250# rand(r, ()) would match both this method and rand(r, dims::Dims)
233251# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
234252
235- rand (r:: AbstractRNG , :: Type{X} , dims:: Dims ) where {X} = rand! (r, Array {eltype (X)} (undef, dims), X)
253+ rand (r:: AbstractRNG , :: Type{X} , dims:: Dims ) where {X} = rand! (r, Array {gentype (X)} (undef, dims), X)
236254rand ( :: Type{X} , dims:: Dims ) where {X} = rand (GLOBAL_RNG, X, dims)
237255
238256rand (r:: AbstractRNG , :: Type{X} , d:: Integer , dims:: Integer... ) where {X} = rand (r, X, Dims ((d, dims... )))
0 commit comments