@@ -34,6 +34,25 @@ export srand,
3434
3535abstract type AbstractRNG end
3636
37+ """
38+ Random.gentype(T)
39+
40+ Determine the type of the elements generated by calling `rand([rng], x)`,
41+ where `x::T`, and `x` is not a type.
42+ The definition `gentype(x) = gentype(typeof(x))` is provided for convenience,
43+ and `gentype(T)` defaults to `eltype(T)`.
44+ NOTE: `rand([rng], X)`, where `X` is a type, is always assumed to produce
45+ an object of type `X`.
46+
47+ # Examples
48+ ```jldoctest
49+ julia> gentype(1:10)
50+ Int64
51+ ```
52+ """
53+ gentype (:: Type{X} ) where {X} = eltype (X)
54+ gentype (x) = gentype (typeof (x))
55+
3756
3857# ## integers
3958
@@ -72,7 +91,7 @@ for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw,
7291 end
7392end
7493
75- Base . eltype (:: Type{<:UniformBits{T}} ) where {T} = T
94+ gentype (:: Type{<:UniformBits{T}} ) where {T} = T
7695
7796# ## floats
7897
@@ -88,15 +107,15 @@ const CloseOpen12_64 = CloseOpen12{Float64}
88107CloseOpen01 (:: Type{T} = Float64) where {T<: AbstractFloat } = CloseOpen01 {T} ()
89108CloseOpen12 (:: Type{T} = Float64) where {T<: AbstractFloat } = CloseOpen12 {T} ()
90109
91- Base . eltype (:: Type{<:FloatInterval{T}} ) where {T<: AbstractFloat } = T
110+ gentype (:: Type{<:FloatInterval{T}} ) where {T<: AbstractFloat } = T
92111
93112const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
94113
95114# ## Sampler
96115
97116abstract type Sampler{E} end
98117
99- Base . eltype (:: Type{<:Sampler{E}} ) where {E} = E
118+ gentype (:: Type{<:Sampler{E}} ) where {E} = E
100119
101120# temporarily for BaseBenchmarks
102121RangeGenerator (x) = Sampler (GLOBAL_RNG, x)
@@ -133,7 +152,7 @@ struct SamplerTrivial{T,E} <: Sampler{E}
133152 self:: T
134153end
135154
136- SamplerTrivial (x:: T ) where {T} = SamplerTrivial {T,eltype (T)} (x)
155+ SamplerTrivial (x:: T ) where {T} = SamplerTrivial {T,gentype (T)} (x)
137156
138157Sampler (:: AbstractRNG , x, :: Repetition ) = SamplerTrivial (x)
139158
@@ -145,14 +164,14 @@ struct SamplerSimple{T,S,E} <: Sampler{E}
145164 data:: S
146165end
147166
148- SamplerSimple (x:: T , data:: S ) where {T,S} = SamplerSimple {T,S,eltype (T)} (x, data)
167+ SamplerSimple (x:: T , data:: S ) where {T,S} = SamplerSimple {T,S,gentype (T)} (x, data)
149168
150169Base. getindex (sp:: SamplerSimple ) = sp. self
151170
152171# simple sampler carrying a (type) tag T and data
153172struct SamplerTag{T,S,E} <: Sampler{E}
154173 data:: S
155- SamplerTag {T} (s:: S ) where {T,S} = new {T,S,eltype (T)} (s)
174+ SamplerTag {T} (s:: S ) where {T,S} = new {T,S,gentype (T)} (s)
156175end
157176
158177
223242rand (r:: AbstractRNG , dims:: Integer... ) = rand (r, Float64, Dims (dims))
224243rand ( dims:: Integer... ) = rand (Float64, Dims (dims))
225244
226- rand (r:: AbstractRNG , X, dims:: Dims ) = rand! (r, Array {eltype (X)} (undef, dims), X)
245+ rand (r:: AbstractRNG , X, dims:: Dims ) = rand! (r, Array {gentype (X)} (undef, dims), X)
227246rand ( X, dims:: Dims ) = rand (GLOBAL_RNG, X, dims)
228247
229248rand (r:: AbstractRNG , X, d:: Integer , dims:: Integer... ) = rand (r, X, Dims ((d, dims... )))
@@ -232,7 +251,7 @@ rand( X, d::Integer, dims::Integer...) = rand(X, Dims((d, dims...
232251# rand(r, ()) would match both this method and rand(r, dims::Dims)
233252# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
234253
235- rand (r:: AbstractRNG , :: Type{X} , dims:: Dims ) where {X} = rand! (r, Array {eltype(X) } (undef, dims), X)
254+ rand (r:: AbstractRNG , :: Type{X} , dims:: Dims ) where {X} = rand! (r, Array {X } (undef, dims), X)
236255rand ( :: Type{X} , dims:: Dims ) where {X} = rand (GLOBAL_RNG, X, dims)
237256
238257rand (r:: AbstractRNG , :: Type{X} , d:: Integer , dims:: Integer... ) where {X} = rand (r, X, Dims ((d, dims... )))
0 commit comments