@@ -260,27 +260,26 @@ function _rand(rng, p_cumulative, R)
260
260
return index
261
261
end
262
262
263
- function Base. rand (rng:: AbstractRNG ,
264
- d:: UnivariateFinite{<:Any,<:Any,R} ) where R
265
- p_cumulative = _cumulative (d)
266
- return Dist. support (d)[_rand (rng, p_cumulative, R)]
267
- end
268
- Base. rand (d:: UnivariateFinite ) = rand (Random. default_rng (), d)
269
-
270
- function Base. rand (rng:: AbstractRNG ,
271
- d:: UnivariateFinite{<:Any,<:Any,R} ,
272
- dim1:: Integer , moredims:: Integer... ) where R # ref type
273
- p_cumulative = _cumulative (d)
274
- A = Array {R} (undef, dim1, moredims... )
275
- for i in eachindex (A)
276
- @inbounds A[i] = _rand (rng, p_cumulative, R)
277
- end
278
- support = Dist. support (d)
279
- return broadcast (i -> support[i], A)
263
+ Random. eltype (:: Type{<:UnivariateFinite{<:Any,V}} ) where V = V
264
+
265
+ # The Sampler hook into Random's API is discussed in the Julia documentation, in the
266
+ # Standard Library section on Random.
267
+ function Random. Sampler (
268
+ :: AbstractRNG ,
269
+ d:: UnivariateFinite ,
270
+ :: Random.Repetition ,
271
+ )
272
+ data = (_cumulative (d), Dist. support (d))
273
+ Random. SamplerSimple (d, data)
280
274
end
281
275
282
- Base. rand (d:: UnivariateFinite , dim1:: Integer , moredims:: Integer... ) =
283
- rand (Random. default_rng (), d, dim1, moredims... )
276
+ function Base. rand (
277
+ rng:: AbstractRNG ,
278
+ sampler:: Random.SamplerSimple{<:UnivariateFinite{<:Any,<:Any,R}} ,
279
+ ) where R
280
+ p_cumulative, support = sampler. data
281
+ return support[_rand (rng, p_cumulative, R)]
282
+ end
284
283
285
284
function Dist. fit (d:: Type{<:UnivariateFinite} ,
286
285
v:: AbstractVector{C} ) where C
0 commit comments