@@ -30,35 +30,23 @@ function rand(rng::AbstractRNG, s::Sampleable{<:ArrayLikeVariate})
3030end
3131
3232# multiple samples
33- function rand (rng:: AbstractRNG , s:: Sampleable{Univariate} , dims:: Dims )
34- out = Array {eltype(s)} (undef, dims)
35- return @inbounds rand! (rng, sampler (s), out)
33+ # we use function barriers since for some distributions `sampler(s)` is not type-stable:
34+ # https://github.com/JuliaStats/Distributions.jl/pull/1281
35+ function rand (rng:: AbstractRNG , s:: Sampleable{<:ArrayLikeVariate} , dims:: Dims )
36+ return _rand (rng, sampler (s), dims)
3637end
37- function rand (
38- rng:: AbstractRNG , s:: Sampleable{<:ArrayLikeVariate} , dims:: Dims ,
39- )
40- sz = size (s)
41- ax = map (Base. OneTo, dims)
42- out = [Array {eltype(s)} (undef, sz) for _ in Iterators. product (ax... )]
43- return @inbounds rand! (rng, sampler (s), out, false )
38+ function _rand (rng:: AbstractRNG , s:: Sampleable{<:ArrayLikeVariate} , dims:: Dims )
39+ r = rand (rng, s)
40+ out = Array {typeof(r)} (undef, dims)
41+ out[begin ] = r
42+ rand! (rng, s, @view (out[2 : end ]))
43+ return out
4444end
4545
46- # these are workarounds for sampleables that incorrectly base `eltype` on the parameters
46+ # this is a workaround for sampleables that incorrectly base `eltype` on the parameters
4747function rand (rng:: AbstractRNG , s:: Sampleable{<:ArrayLikeVariate,Continuous} )
4848 return @inbounds rand! (rng, sampler (s), Array {float(eltype(s))} (undef, size (s)))
4949end
50- function rand (rng:: AbstractRNG , s:: Sampleable{Univariate,Continuous} , dims:: Dims )
51- out = Array {float(eltype(s))} (undef, dims)
52- return @inbounds rand! (rng, sampler (s), out)
53- end
54- function rand (
55- rng:: AbstractRNG , s:: Sampleable{<:ArrayLikeVariate,Continuous} , dims:: Dims ,
56- )
57- sz = size (s)
58- ax = map (Base. OneTo, dims)
59- out = [Array {float(eltype(s))} (undef, sz) for _ in Iterators. product (ax... )]
60- return @inbounds rand! (rng, sampler (s), out, false )
61- end
6250
6351"""
6452 rand!([rng::AbstractRNG,] s::Sampleable, A::AbstractArray)
0 commit comments