• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37474

pending completion
#37474

push

local

web-flow
irinterp: Allow setting all IR flags (#48993)

Currently, `IR_FLAG_NOTHROW` is the only flag that irinterp is allowed to
set on statements, under the assumption that in order for a call to
be irinterp-eligible, it must have been proven `:foldable`, thus `:effect_free`,
and thus `IR_FLAG_EFFECT_FREE` was assumed to have been set. That reasoning
was sound at the time this code was written, but have since introduced
`EFFECT_FREE_IF_INACCESSIBLEMEMONLY`, which breaks the reasoning that
an `:effect_free` inference for the whole function implies the flag on
every statement. As a result, we were failing to DCE otherwise dead
statements if the IR came from irinterp.

3 of 3 new or added lines in 1 file covered. (100.0%)

70258 of 82316 relevant lines covered (85.35%)

32461773.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.07
/stdlib/Random/src/generation.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# Uniform random generation
4

5
# This file contains the creation of Sampler objects and the associated generation of
6
# random values from them. More specifically, given the specification S of a set
7
# of values to pick from (e.g. 1:10, or "a string"), we define
8
#
9
# 1) Sampler(rng, S, ::Repetition) -> sampler
10
# 2) rand(rng, sampler) -> random value
11
#
12
# Note that the 1) is automated when the sampler is not intended to carry information,
13
# i.e. the default fall-backs SamplerType and SamplerTrivial are used.
14

15
## from types: rand(::Type, [dims...])
16

17
### random floats
18

19
Sampler(::Type{RNG}, ::Type{T}, n::Repetition) where {RNG<:AbstractRNG,T<:AbstractFloat} =
44,926,434✔
20
    Sampler(RNG, CloseOpen01(T), n)
21

22
# generic random generation function which can be used by RNG implementors
23
# it is not defined as a fallback rand method as this could create ambiguities
24

25
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01{Float16}}) =
6,143✔
26
    Float16(reinterpret(Float32,
27
                        (rand(r, UInt10(UInt32)) << 13)  | 0x3f800000) - 1)
28

29
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01{Float32}}) =
6,115✔
30
    reinterpret(Float32, rand(r, UInt23()) | 0x3f800000) - 1
31

32
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen12_64}) =
44,454✔
33
    reinterpret(Float64, 0x3ff0000000000000 | rand(r, UInt52()))
34

35
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01_64}) = rand(r, CloseOpen12()) - 1.0
417,183✔
36

37
#### BigFloat
38

39
const bits_in_Limb = sizeof(Limb) << 3
40
const Limb_high_bit = one(Limb) << (bits_in_Limb-1)
41

42
struct SamplerBigFloat{I<:FloatInterval{BigFloat}} <: Sampler{BigFloat}
43
    prec::Int
44
    nlimbs::Int
45
    limbs::Vector{Limb}
46
    shift::UInt
47

48
    function SamplerBigFloat{I}(prec::Int) where I<:FloatInterval{BigFloat}
42,943✔
49
        nlimbs = (prec-1) ÷ bits_in_Limb + 1
42,943✔
50
        limbs = Vector{Limb}(undef, nlimbs)
42,943✔
51
        shift = nlimbs * bits_in_Limb - prec
42,943✔
52
        new(prec, nlimbs, limbs, shift)
42,943✔
53
    end
54
end
55

56
Sampler(::Type{<:AbstractRNG}, I::FloatInterval{BigFloat}, ::Repetition) =
42,943✔
57
    SamplerBigFloat{typeof(I)}(precision(BigFloat))
58

59
function _rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat)
82,961✔
60
    precision(z) == sp.prec || throw(ArgumentError("incompatible BigFloat precision"))
82,963✔
61
    limbs = sp.limbs
82,959✔
62
    rand!(rng, limbs)
82,959✔
63
    @inbounds begin
82,959✔
64
        limbs[1] <<= sp.shift
82,959✔
65
        randbool = iszero(limbs[end] & Limb_high_bit)
82,959✔
66
        limbs[end] |= Limb_high_bit
82,959✔
67
    end
68
    z.sign = 1
82,959✔
69
    GC.@preserve limbs unsafe_copyto!(z.d, pointer(limbs), sp.nlimbs)
82,959✔
70
    randbool
82,959✔
71
end
72

73
function _rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat, ::CloseOpen12{BigFloat})
3✔
74
    _rand!(rng, z, sp)
3✔
75
    z.exp = 1
2✔
76
    z
2✔
77
end
78

79
function _rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat, ::CloseOpen01{BigFloat})
82,958✔
80
    randbool = _rand!(rng, z, sp)
82,958✔
81
    z.exp = 0
82,957✔
82
    randbool &&
82,957✔
83
        ccall((:mpfr_sub_d, :libmpfr), Int32,
84
              (Ref{BigFloat}, Ref{BigFloat}, Cdouble, Base.MPFR.MPFRRoundingMode),
85
              z, z, 0.5, Base.MPFR.ROUNDING_MODE[])
86
    z
82,957✔
87
end
88

89
# alternative, with 1 bit less of precision
90
# TODO: make an API for requesting full or not-full precision
91
function _rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat, ::CloseOpen01{BigFloat},
×
92
                ::Nothing)
93
    _rand!(rng, z, sp, CloseOpen12(BigFloat))
×
94
    ccall((:mpfr_sub_ui, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Culong, Base.MPFR.MPFRRoundingMode),
×
95
          z, z, 1, Base.MPFR.ROUNDING_MODE[])
96
    z
×
97
end
98

99
rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat{T}
100
      ) where {T<:FloatInterval{BigFloat}} =
82,961✔
101
          _rand!(rng, z, sp, T())
102

103
rand(rng::AbstractRNG, sp::SamplerBigFloat{T}) where {T<:FloatInterval{BigFloat}} =
82,957✔
104
    rand!(rng, BigFloat(; precision=sp.prec), sp)
105

106

107
### random integers
108

109
#### UniformBits
110

111
rand(r::AbstractRNG, ::SamplerTrivial{UInt10Raw{UInt16}}) = rand(r, UInt16)
6,143✔
112
rand(r::AbstractRNG, ::SamplerTrivial{UInt23Raw{UInt32}}) = rand(r, UInt32)
6,115✔
113

114
rand(r::AbstractRNG, ::SamplerTrivial{UInt52Raw{UInt64}}) =
5,213✔
115
    _rand52(r, rng_native_52(r))
116

117
_rand52(r::AbstractRNG, ::Type{Float64}) = reinterpret(UInt64, rand(r, CloseOpen12()))
×
118
_rand52(r::AbstractRNG, ::Type{UInt64})  = rand(r, UInt64)
5,213✔
119

120
rand(r::AbstractRNG, ::SamplerTrivial{UInt104Raw{UInt128}}) =
2✔
121
    rand(r, UInt52Raw(UInt128)) << 52 ⊻ rand(r, UInt52Raw(UInt128))
122

123
rand(r::AbstractRNG, ::SamplerTrivial{UInt10{UInt16}})   = rand(r, UInt10Raw())  & 0x03ff
6,143✔
124
rand(r::AbstractRNG, ::SamplerTrivial{UInt23{UInt32}})   = rand(r, UInt23Raw())  & 0x007fffff
6,115✔
125
rand(r::AbstractRNG, ::SamplerTrivial{UInt52{UInt64}})   = rand(r, UInt52Raw())  & 0x000fffffffffffff
6,335✔
126
rand(r::AbstractRNG, ::SamplerTrivial{UInt104{UInt128}}) = rand(r, UInt104Raw()) & 0x000000ffffffffffffffffffffffffff
×
127

128
rand(r::AbstractRNG, sp::SamplerTrivial{<:UniformBits{T}}) where {T} =
1,554,293✔
129
        rand(r, uint_default(sp[])) % T
130

131
#### BitInteger
132

133
# rand_generic methods are intended to help RNG implementors with common operations
134
# we don't call them simply `rand` as this can easily contribute to create
135
# ambiguities with user-side methods (forcing the user to resort to @eval)
136

137
rand_generic(r::AbstractRNG, T::Union{Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32}) =
×
138
    rand(r, UInt52Raw()) % T[]
139

140
rand_generic(r::AbstractRNG, ::Type{UInt64}) =
×
141
    rand(r, UInt52Raw()) << 32 ⊻ rand(r, UInt52Raw())
142

143
rand_generic(r::AbstractRNG, ::Type{UInt128}) = _rand128(r, rng_native_52(r))
×
144

145
_rand128(r::AbstractRNG, ::Type{UInt64}) =
×
146
    ((rand(r, UInt64) % UInt128) << 64) ⊻ rand(r, UInt64)
147

148
function _rand128(r::AbstractRNG, ::Type{Float64})
×
149
    xor(rand(r, UInt52Raw(UInt128))  << 96,
×
150
        rand(r, UInt52Raw(UInt128))  << 48,
151
        rand(r, UInt52Raw(UInt128)))
152
end
153

154
rand_generic(r::AbstractRNG, ::Type{Int128}) = rand(r, UInt128) % Int128
×
155
rand_generic(r::AbstractRNG, ::Type{Int64})  = rand(r, UInt64) % Int64
×
156

157
### random complex numbers
158

159
rand(r::AbstractRNG, ::SamplerType{Complex{T}}) where {T<:Real} =
166,178✔
160
    complex(rand(r, T), rand(r, T))
161

162
### random characters
163

164
# returns a random valid Unicode scalar value (i.e. 0 - 0xd7ff, 0xe000 - # 0x10ffff)
165
function rand(r::AbstractRNG, ::SamplerType{T}) where {T<:AbstractChar}
233✔
166
    c = rand(r, 0x00000000:0x0010f7ff)
233✔
167
    (c < 0xd800) ? T(c) : T(c+0x800)
233✔
168
end
169

170

171
## Generate random integer within a range
172

173
### BitInteger
174

175
# there are three implemented samplers for unit ranges, the second one
176
# assumes that Float64 (i.e. 52 random bits) is the native type for the RNG:
177
# 1) "Fast" (SamplerRangeFast), which is most efficient when the range length is close
178
#    (or equal) to a power of 2 from below.
179
#    The tradeoff is faster creation of the sampler, but more consumption of entropy bits.
180
# 2) "Slow" (SamplerRangeInt) which tries to use as few entropy bits as possible, at the
181
#    cost of a bigger upfront price associated with the creation of the sampler.
182
#    This sampler is most appropriate for slower random generators.
183
# 3) "Nearly Division Less" (NDL) which is generally the fastest algorithm for types of size
184
#    up to 64 bits. This is the default for these types since Julia 1.5.
185
#    The "Fast" algorithm can be faster than NDL when the length of the range is
186
#    less than and close to a power of 2.
187

188
Sampler(::Type{<:AbstractRNG}, r::AbstractUnitRange{T},
189
        ::Repetition) where {T<:Base.BitInteger64} = SamplerRangeNDL(r)
2,672,669✔
190

191
Sampler(::Type{<:AbstractRNG}, r::AbstractUnitRange{T},
192
        ::Repetition) where {T<:Union{Int128,UInt128}} = SamplerRangeFast(r)
28✔
193

194
#### helper functions
195

196
uint_sup(::Type{<:Base.BitInteger32}) = UInt32
100,443✔
197
uint_sup(::Type{<:Union{Int64,UInt64}}) = UInt64
144✔
198
uint_sup(::Type{<:Union{Int128,UInt128}}) = UInt128
64✔
199

200
#### Fast
201

202
struct SamplerRangeFast{U<:BitUnsigned,T<:BitInteger} <: Sampler{T}
203
    a::T      # first element of the range
91✔
204
    bw::UInt  # bit width
205
    m::U      # range length - 1
206
    mask::U   # mask generated values before threshold rejection
207
end
208

209
SamplerRangeFast(r::AbstractUnitRange{T}) where T<:BitInteger =
91✔
210
    SamplerRangeFast(r, uint_sup(T))
211

212
function SamplerRangeFast(r::AbstractUnitRange{T}, ::Type{U}) where {T,U}
91✔
213
    isempty(r) && throw(ArgumentError("collection must be non-empty"))
91✔
214
    m = (last(r) - first(r)) % unsigned(T) % U # % unsigned(T) to not propagate sign bit
91✔
215
    bw = (Base.top_set_bit(m)) % UInt # bit-width
91✔
216
    mask = ((1 % U) << bw) - (1 % U)
91✔
217
    SamplerRangeFast{U,T}(first(r), bw, m, mask)
91✔
218
end
219

220
function rand(rng::AbstractRNG, sp::SamplerRangeFast{UInt32,T}) where T
36✔
221
    a, bw, m, mask = sp.a, sp.bw, sp.m, sp.mask
36✔
222
    # below, we don't use UInt32, to get reproducible values, whether Int is Int64 or Int32
223
    x = rand(rng, LessThan(m, Masked(mask, UInt52Raw(UInt32))))
43✔
224
    (x + a % UInt32) % T
36✔
225
end
226

227
has_fast_64(rng::AbstractRNG) = rng_native_52(rng) != Float64
57✔
228
# for MersenneTwister, both options have very similar performance
229

230
function rand(rng::AbstractRNG, sp::SamplerRangeFast{UInt64,T}) where T
9✔
231
    a, bw, m, mask = sp.a, sp.bw, sp.m, sp.mask
9✔
232
    if !has_fast_64(rng) && bw <= 52
9✔
233
        x = rand(rng, LessThan(m, Masked(mask, UInt52Raw())))
2✔
234
    else
235
        x = rand(rng, LessThan(m, Masked(mask, uniform(UInt64))))
8✔
236
    end
237
    (x + a % UInt64) % T
9✔
238
end
239

240
function rand(rng::AbstractRNG, sp::SamplerRangeFast{UInt128,T}) where T
48✔
241
    a, bw, m, mask = sp.a, sp.bw, sp.m, sp.mask
48✔
242
    if has_fast_64(rng)
48✔
243
        x = bw <= 64 ?
46✔
244
            rand(rng, LessThan(m % UInt64, Masked(mask % UInt64, uniform(UInt64)))) % UInt128 :
245
            rand(rng, LessThan(m, Masked(mask, uniform(UInt128))))
246
    else
247
        x = bw <= 52  ?
9✔
248
            rand(rng, LessThan(m % UInt64, Masked(mask % UInt64, UInt52Raw()))) % UInt128 :
249
        bw <= 104 ?
250
            rand(rng, LessThan(m, Masked(mask, UInt104Raw()))) :
251
            rand(rng, LessThan(m, Masked(mask, uniform(UInt128))))
252
    end
253
    x % T + a
48✔
254
end
255

256
#### "Slow" / SamplerRangeInt
257

258
# remainder function according to Knuth, where rem_knuth(a, 0) = a
259
rem_knuth(a::UInt, b::UInt) = a % (b + (b == 0)) + a * (b == 0)
9✔
260
rem_knuth(a::T, b::T) where {T<:Unsigned} = b != 0 ? a % b : a
66✔
261

262
# maximum multiple of k <= sup decremented by one,
263
# that is 0xFFFF...FFFF if k = (typemax(T) - typemin(T)) + 1 and sup == typemax(T) - 1
264
# with intentional underflow
265
# see http://stackoverflow.com/questions/29182036/integer-arithmetic-add-1-to-uint-max-and-divide-by-n-without-overflow
266

267
# sup == 0 means typemax(T) + 1
268
maxmultiple(k::T, sup::T=zero(T)) where {T<:Unsigned} =
1,670✔
269
    (div(sup - k, k + (k == 0))*k + k - one(k))::T
270

271
# similar but sup must not be equal to typemax(T)
272
unsafe_maxmultiple(k::T, sup::T) where {T<:Unsigned} =
118✔
273
    div(sup, k + (k == 0))*k - one(k)
274

275
struct SamplerRangeInt{T<:Integer,U<:Unsigned} <: Sampler{T}
276
    a::T      # first element of the range
185✔
277
    bw::Int   # bit width
278
    k::U      # range length or zero for full range
279
    u::U      # rejection threshold
280
end
281

282

283
SamplerRangeInt(r::AbstractUnitRange{T}) where T<:BitInteger =
185✔
284
    SamplerRangeInt(r, uint_sup(T))
285

286
function SamplerRangeInt(r::AbstractUnitRange{T}, ::Type{U}) where {T,U}
185✔
287
    isempty(r) && throw(ArgumentError("collection must be non-empty"))
185✔
288
    a = first(r)
185✔
289
    m = (last(r) - first(r)) % unsigned(T) % U
185✔
290
    k = m + one(U)
185✔
291
    bw = (Base.top_set_bit(m)) % Int
185✔
292
    mult = if U === UInt32
185✔
293
        maxmultiple(k)
36✔
294
    elseif U === UInt64
149✔
295
        bw <= 52 ? unsafe_maxmultiple(k, one(UInt64) << 52) :
156✔
296
                   maxmultiple(k)
297
    else # U === UInt128
298
        bw <= 52  ? unsafe_maxmultiple(k, one(UInt128) << 52) :
24✔
299
        bw <= 104 ? unsafe_maxmultiple(k, one(UInt128) << 104) :
300
                    maxmultiple(k)
301
    end
302

303
    SamplerRangeInt{T,U}(a, bw, k, mult) # overflow ok
185✔
304
end
305

306
rand(rng::AbstractRNG, sp::SamplerRangeInt{T,UInt32}) where {T<:BitInteger} =
36✔
307
    (unsigned(sp.a) + rem_knuth(rand(rng, LessThan(sp.u, UInt52Raw(UInt32))), sp.k)) % T
308

309
# this function uses 52 bit entropy for small ranges of length <= 2^52
310
function rand(rng::AbstractRNG, sp::SamplerRangeInt{T,UInt64}) where T<:BitInteger
9✔
311
    x = sp.bw <= 52 ? rand(rng, LessThan(sp.u, UInt52())) :
10✔
312
                      rand(rng, LessThan(sp.u, uniform(UInt64)))
313
    return ((sp.a % UInt64) + rem_knuth(x, sp.k)) % T
9✔
314
end
315

316
function rand(rng::AbstractRNG, sp::SamplerRangeInt{T,UInt128}) where T<:BitInteger
18✔
317
    x = sp.bw <= 52  ? rand(rng, LessThan(sp.u, UInt52(UInt128))) :
20✔
318
        sp.bw <= 104 ? rand(rng, LessThan(sp.u, UInt104(UInt128))) :
319
                       rand(rng, LessThan(sp.u, uniform(UInt128)))
320
    return ((sp.a % UInt128) + rem_knuth(x, sp.k)) % T
18✔
321
end
322

323
#### Nearly Division Less
324

325
# cf. https://arxiv.org/abs/1805.10941 (algorithm 5)
326

327
struct SamplerRangeNDL{U<:Unsigned,T} <: Sampler{T}
328
    a::T  # first element of the range
2,643,760✔
329
    s::U  # range length or zero for full range
330
end
331

332
function SamplerRangeNDL(r::AbstractUnitRange{T}) where {T}
1,216,380✔
333
    isempty(r) && throw(ArgumentError("collection must be non-empty"))
1,321,930✔
334
    a = first(r)
113,339✔
335
    U = uint_sup(T)
100,458✔
336
    s = (last(r) - first(r)) % unsigned(T) % U + one(U) # overflow ok
1,321,882✔
337
    # mod(-s, s) could be put in the Sampler object for repeated calls, but
338
    # this would be an advantage only for very big s and number of calls
339
    SamplerRangeNDL(a, s)
1,321,882✔
340
end
341

342
function rand(rng::AbstractRNG, sp::SamplerRangeNDL{U,T}) where {U,T}
6,956,476✔
343
    s = sp.s
6,956,476✔
344
    x = widen(rand(rng, U))
6,956,476✔
345
    m = x * s
6,956,476✔
346
    l = m % U
6,956,476✔
347
    if l < s
6,956,476✔
348
        t = mod(-s, s) # as s is unsigned, -s is equal to 2^L - s in the paper
19✔
349
        while l < t
27✔
350
            x = widen(rand(rng, U))
8✔
351
            m = x * s
8✔
352
            l = m % U
8✔
353
        end
8✔
354
    end
355
    (s == 0 ? x : m >> (8*sizeof(U))) % T + sp.a
6,956,476✔
356
end
357

358

359
### BigInt
360

361
struct SamplerBigInt{SP<:Sampler{Limb}} <: Sampler{BigInt}
362
    a::BigInt         # first
136✔
363
    m::BigInt         # range length - 1
364
    nlimbs::Int       # number of limbs in generated BigInt's (z ∈ [0, m])
365
    nlimbsmax::Int    # max number of limbs for z+a
366
    highsp::SP        # sampler for the highest limb of z
367
end
368

369
function SamplerBigInt(::Type{RNG}, r::AbstractUnitRange{BigInt}, N::Repetition=Val(Inf)
69✔
370
                       ) where {RNG<:AbstractRNG}
371
    m = last(r) - first(r)
69✔
372
    m.size < 0 && throw(ArgumentError("collection must be non-empty"))
68✔
373
    nlimbs = Int(m.size)
68✔
374
    hm = nlimbs == 0 ? Limb(0) : GC.@preserve m unsafe_load(m.d, nlimbs)
133✔
375
    highsp = Sampler(RNG, Limb(0):hm, N)
68✔
376
    nlimbsmax = max(nlimbs, abs(last(r).size), abs(first(r).size))
68✔
377
    return SamplerBigInt(first(r), m, nlimbs, nlimbsmax, highsp)
68✔
378
end
379

380
Sampler(::Type{RNG}, r::AbstractUnitRange{BigInt}, N::Repetition) where {RNG<:AbstractRNG} =
67✔
381
    SamplerBigInt(RNG, r, N)
382

383
rand(rng::AbstractRNG, sp::SamplerBigInt) =
8,065✔
384
    rand!(rng, BigInt(nbits = sp.nlimbsmax*8*sizeof(Limb)), sp)
385

386
function rand!(rng::AbstractRNG, x::BigInt, sp::SamplerBigInt)
8,072✔
387
    nlimbs = sp.nlimbs
8,072✔
388
    nlimbs == 0 && return MPZ.set!(x, sp.a)
8,072✔
389
    MPZ.realloc2!(x, sp.nlimbsmax*8*sizeof(Limb))
8,066✔
390
    @assert x.alloc >= nlimbs
8,066✔
391
    # we randomize x ∈ [0, m] with rejection sampling:
392
    # 1. the first nlimbs-1 limbs of x are uniformly randomized
393
    # 2. the high limb hx of x is sampled from 0:hm where hm is the
394
    #    high limb of m
395
    # We repeat 1. and 2. until x <= m
396
    hm = GC.@preserve sp unsafe_load(sp.m.d, nlimbs)
8,066✔
397
    GC.@preserve x begin
8,066✔
398
        limbs = UnsafeView(x.d, nlimbs-1)
8,066✔
399
        while true
8,066✔
400
            rand!(rng, limbs)
8,066✔
401
            hx = limbs[nlimbs] = rand(rng, sp.highsp)
8,066✔
402
            hx < hm && break # avoid calling mpn_cmp most of the time
8,066✔
403
            MPZ.mpn_cmp(x, sp.m, nlimbs) <= 0 && break
1✔
404
        end
×
405
        # adjust x.size (normally done by mpz_limbs_finish, in GMP version >= 6)
406
        while nlimbs > 0
8,070✔
407
            limbs[nlimbs] != 0 && break
8,067✔
408
            nlimbs -= 1
4✔
409
        end
4✔
410
        x.size = nlimbs
8,066✔
411
    end
412
    MPZ.add!(x, sp.a)
8,066✔
413
end
414

415

416
## random values from AbstractArray
417

418
Sampler(::Type{RNG}, r::AbstractArray, n::Repetition) where {RNG<:AbstractRNG} =
1,205,657✔
419
    SamplerSimple(r, Sampler(RNG, firstindex(r):lastindex(r), n))
420

421
rand(rng::AbstractRNG, sp::SamplerSimple{<:AbstractArray,<:Sampler}) =
1,966,828✔
422
    @inbounds return sp[][rand(rng, sp.data)]
423

424

425
## random values from Dict
426

427
function Sampler(::Type{RNG}, t::Dict, ::Repetition) where RNG<:AbstractRNG
329✔
428
    isempty(t) && throw(ArgumentError("collection must be non-empty"))
329✔
429
    # we use Val(Inf) below as rand is called repeatedly internally
430
    # even for generating only one random value from t
431
    SamplerSimple(t, Sampler(RNG, LinearIndices(t.slots), Val(Inf)))
313✔
432
end
433

434
function rand(rng::AbstractRNG, sp::SamplerSimple{<:Dict,<:Sampler})
2,704✔
435
    while true
6,140✔
436
        i = rand(rng, sp.data)
6,140✔
437
        Base.isslotfilled(sp[], i) && @inbounds return (sp[].keys[i] => sp[].vals[i])
6,140✔
438
    end
3,436✔
439
end
440

441
## random values from Set
442

443
Sampler(::Type{RNG}, t::Set{T}, n::Repetition) where {RNG<:AbstractRNG,T} =
58✔
444
    SamplerTag{Set{T}}(Sampler(RNG, t.dict, n))
445

446
rand(rng::AbstractRNG, sp::SamplerTag{<:Set,<:Sampler}) = rand(rng, sp.data).first
603✔
447

448
## random values from BitSet
449

450
function Sampler(RNG::Type{<:AbstractRNG}, t::BitSet, n::Repetition)
54✔
451
    isempty(t) && throw(ArgumentError("collection must be non-empty"))
54✔
452
    SamplerSimple(t, Sampler(RNG, minimum(t):maximum(t), Val(Inf)))
46✔
453
end
454

455
function rand(rng::AbstractRNG, sp::SamplerSimple{BitSet,<:Sampler})
230✔
456
    while true
1,154✔
457
        n = rand(rng, sp.data)
1,154✔
458
        n in sp[] && return n
1,154✔
459
    end
924✔
460
end
461

462
## random values from AbstractDict/AbstractSet
463

464
# we defer to _Sampler to avoid ambiguities with a call like Sampler(rng, Set(1), Val(1))
465
Sampler(RNG::Type{<:AbstractRNG}, t::Union{AbstractDict,AbstractSet}, n::Repetition) =
941✔
466
    _Sampler(RNG, t, n)
467

468
# avoid linear complexity for repeated calls
469
_Sampler(RNG::Type{<:AbstractRNG}, t::Union{AbstractDict,AbstractSet}, n::Val{Inf}) =
96✔
470
    Sampler(RNG, collect(t), n)
471

472
# when generating only one element, avoid the call to collect
473
_Sampler(::Type{<:AbstractRNG}, t::Union{AbstractDict,AbstractSet}, ::Val{1}) =
845✔
474
    SamplerTrivial(t)
475

476
function nth(iter, n::Integer)::eltype(iter)
837✔
477
    for (i, x) in enumerate(iter)
1,674✔
478
        i == n && return x
2,081✔
479
    end
1,244✔
480
end
481

482
rand(rng::AbstractRNG, sp::SamplerTrivial{<:Union{AbstractDict,AbstractSet}}) =
845✔
483
    nth(sp[], rand(rng, 1:length(sp[])))
484

485

486
## random characters from a string
487

488
# we use collect(str), which is most of the time more efficient than specialized methods
489
# (except maybe for very small arrays)
490
Sampler(RNG::Type{<:AbstractRNG}, str::AbstractString, n::Val{Inf}) = Sampler(RNG, collect(str), n)
1,196✔
491

492
# when generating only one char from a string, the specialized method below
493
# is usually more efficient
494
Sampler(RNG::Type{<:AbstractRNG}, str::AbstractString, ::Val{1}) =
24✔
495
    SamplerSimple(str, Sampler(RNG, 1:_lastindex(str), Val(Inf)))
496

497
isvalid_unsafe(s::String, i) = !Base.is_valid_continuation(GC.@preserve s unsafe_load(pointer(s), i))
5✔
498
isvalid_unsafe(s::AbstractString, i) = isvalid(s, i)
4✔
499
_lastindex(s::String) = sizeof(s)
8✔
500
_lastindex(s::AbstractString) = lastindex(s)
8✔
501

502
function rand(rng::AbstractRNG, sp::SamplerSimple{<:AbstractString,<:Sampler})::Char
8✔
503
    str = sp[]
8✔
504
    while true
9✔
505
        pos = rand(rng, sp.data)
9✔
506
        isvalid_unsafe(str, pos) && return str[pos]
9✔
507
    end
1✔
508
end
509

510

511
## random elements from tuples
512

513
### 1
514

515
Sampler(::Type{<:AbstractRNG}, t::Tuple{A}, ::Repetition) where {A} =
2✔
516
    SamplerTrivial(t)
517

518
rand(rng::AbstractRNG, sp::SamplerTrivial{Tuple{A}}) where {A} =
2✔
519
    @inbounds return sp[][1]
520

521
### 2
522

523
Sampler(RNG::Type{<:AbstractRNG}, t::Tuple{A,B}, n::Repetition) where {A,B} =
12✔
524
    SamplerSimple(t, Sampler(RNG, Bool, n))
525

526
rand(rng::AbstractRNG, sp::SamplerSimple{Tuple{A,B}}) where {A,B} =
202✔
527
    @inbounds return sp[][1 + rand(rng, sp.data)]
528

529
### 3
530

531
Sampler(RNG::Type{<:AbstractRNG}, t::Tuple{A,B,C}, n::Repetition) where {A,B,C} =
2✔
532
    SamplerSimple(t, Sampler(RNG, UInt52(), n))
533

534
function rand(rng::AbstractRNG, sp::SamplerSimple{Tuple{A,B,C}}) where {A,B,C}
2✔
535
    local r
2✔
536
    while true
2✔
537
        r = rand(rng, sp.data)
2✔
538
        r != 0x000fffffffffffff && break # _very_ likely
2✔
539
    end
×
540
    @inbounds return sp[][1 + r ÷ 0x0005555555555555]
2✔
541
end
542

543
### n
544

545
@generated function Sampler(RNG::Type{<:AbstractRNG}, t::Tuple, n::Repetition)
4,012✔
546
    l = fieldcount(t)
14✔
547
    if l < typemax(UInt32) && ispow2(l)
14✔
548
        :(SamplerSimple(t, Sampler(RNG, UInt32, n)))
8✔
549
    else
550
        :(SamplerSimple(t, Sampler(RNG, Base.OneTo(length(t)), n)))
20✔
551
    end
552
end
553

554
@generated function rand(rng::AbstractRNG, sp::SamplerSimple{T}) where T<:Tuple
4,012✔
555
    l = fieldcount(T)
11✔
556
    if l < typemax(UInt32) && ispow2(l)
11✔
557
        quote
5✔
558
            r = rand(rng, sp.data) & ($l-1)
2,002✔
559
            @inbounds return sp[][1 + r]
2,002✔
560
        end
561
    else
562
        :(@inbounds return sp[][rand(rng, sp.data)])
17✔
563
    end
564
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc