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

JuliaLang / julia / #37518

pending completion
#37518

push

local

web-flow
improve effects of  `objectid` and `getindex(::Dict)` (#49447)

This commit also marks `Module` type as `identityfree`.

Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>

10 of 10 new or added lines in 2 files covered. (100.0%)

72092 of 83276 relevant lines covered (86.57%)

31697257.14 hits per line

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

54.35
/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} =
43,366,009✔
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}}) =
×
26
    Float16(reinterpret(Float32,
27
                        (rand(r, UInt10(UInt32)) << 13)  | 0x3f800000) - 1)
28

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

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

35
rand(r::AbstractRNG, ::SamplerTrivial{CloseOpen01_64}) = rand(r, CloseOpen12()) - 1.0
4,538✔
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}
6,220✔
49
        nlimbs = (prec-1) ÷ bits_in_Limb + 1
6,220✔
50
        limbs = Vector{Limb}(undef, nlimbs)
6,220✔
51
        shift = nlimbs * bits_in_Limb - prec
6,220✔
52
        new(prec, nlimbs, limbs, shift)
6,220✔
53
    end
54
end
55

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

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

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

79
function _rand!(rng::AbstractRNG, z::BigFloat, sp::SamplerBigFloat, ::CloseOpen01{BigFloat})
9,872✔
80
    randbool = _rand!(rng, z, sp)
9,872✔
81
    z.exp = 0
9,872✔
82
    randbool &&
9,872✔
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
9,872✔
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}} =
9,872✔
101
          _rand!(rng, z, sp, T())
102

103
rand(rng::AbstractRNG, sp::SamplerBigFloat{T}) where {T<:FloatInterval{BigFloat}} =
9,872✔
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)
×
112
rand(r::AbstractRNG, ::SamplerTrivial{UInt23Raw{UInt32}}) = rand(r, UInt32)
×
113

114
rand(r::AbstractRNG, ::SamplerTrivial{UInt52Raw{UInt64}}) =
21✔
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)
21✔
119

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

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

128
rand(r::AbstractRNG, sp::SamplerTrivial{<:UniformBits{T}}) where {T} =
49,482✔
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} =
173,973✔
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}
4✔
166
    c = rand(r, 0x00000000:0x0010f7ff)
4✔
167
    (c < 0xd800) ? T(c) : T(c+0x800)
4✔
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,568,110✔
190

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

194
#### helper functions
195

196
uint_sup(::Type{<:Base.BitInteger32}) = UInt32
93✔
197
uint_sup(::Type{<:Union{Int64,UInt64}}) = UInt64
43✔
198
uint_sup(::Type{<:Union{Int128,UInt128}}) = UInt128
22✔
199

200
#### Fast
201

202
struct SamplerRangeFast{U<:BitUnsigned,T<:BitInteger} <: Sampler{T}
203
    a::T      # first element of the range
22✔
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 =
22✔
210
    SamplerRangeFast(r, uint_sup(T))
211

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

220
function rand(rng::AbstractRNG, sp::SamplerRangeFast{UInt32,T}) where T
×
221
    a, bw, m, mask = sp.a, sp.bw, sp.m, sp.mask
×
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))))
×
224
    (x + a % UInt32) % T
×
225
end
226

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

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

240
function rand(rng::AbstractRNG, sp::SamplerRangeFast{UInt128,T}) where T
22✔
241
    a, bw, m, mask = sp.a, sp.bw, sp.m, sp.mask
22✔
242
    if has_fast_64(rng)
22✔
243
        x = bw <= 64 ?
22✔
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  ?
×
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
22✔
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)
×
260
rem_knuth(a::T, b::T) where {T<:Unsigned} = b != 0 ? a % b : a
×
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} =
×
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} =
×
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
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 =
×
284
    SamplerRangeInt(r, uint_sup(T))
285

286
function SamplerRangeInt(r::AbstractUnitRange{T}, ::Type{U}) where {T,U}
×
287
    isempty(r) && throw(ArgumentError("collection must be non-empty"))
×
288
    a = first(r)
×
289
    m = (last(r) - first(r)) % unsigned(T) % U
×
290
    k = m + one(U)
×
291
    bw = (Base.top_set_bit(m)) % Int
×
292
    mult = if U === UInt32
×
293
        maxmultiple(k)
×
294
    elseif U === UInt64
×
295
        bw <= 52 ? unsafe_maxmultiple(k, one(UInt64) << 52) :
×
296
                   maxmultiple(k)
297
    else # U === UInt128
298
        bw <= 52  ? unsafe_maxmultiple(k, one(UInt128) << 52) :
×
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
×
304
end
305

306
rand(rng::AbstractRNG, sp::SamplerRangeInt{T,UInt32}) where {T<:BitInteger} =
×
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
×
311
    x = sp.bw <= 52 ? rand(rng, LessThan(sp.u, UInt52())) :
×
312
                      rand(rng, LessThan(sp.u, uniform(UInt64)))
313
    return ((sp.a % UInt64) + rem_knuth(x, sp.k)) % T
×
314
end
315

316
function rand(rng::AbstractRNG, sp::SamplerRangeInt{T,UInt128}) where T<:BitInteger
×
317
    x = sp.bw <= 52  ? rand(rng, LessThan(sp.u, UInt52(UInt128))) :
×
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
×
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
1,219,304✔
329
    s::U  # range length or zero for full range
330
end
331

332
function SamplerRangeNDL(r::AbstractUnitRange{T}) where {T}
1,114,093✔
333
    isempty(r) && throw(ArgumentError("collection must be non-empty"))
1,219,304✔
334
    a = first(r)
12,868✔
335
    U = uint_sup(T)
136✔
336
    s = (last(r) - first(r)) % unsigned(T) % U + one(U) # overflow ok
1,219,304✔
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,219,304✔
340
end
341

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

358

359
### BigInt
360

361
struct SamplerBigInt{SP<:Sampler{Limb}} <: Sampler{BigInt}
362
    a::BigInt         # first
31✔
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)
31✔
370
                       ) where {RNG<:AbstractRNG}
371
    m = last(r) - first(r)
31✔
372
    m.size < 0 && throw(ArgumentError("collection must be non-empty"))
31✔
373
    nlimbs = Int(m.size)
31✔
374
    hm = nlimbs == 0 ? Limb(0) : GC.@preserve m unsafe_load(m.d, nlimbs)
62✔
375
    highsp = Sampler(RNG, Limb(0):hm, N)
31✔
376
    nlimbsmax = max(nlimbs, abs(last(r).size), abs(first(r).size))
31✔
377
    return SamplerBigInt(first(r), m, nlimbs, nlimbsmax, highsp)
31✔
378
end
379

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

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

386
function rand!(rng::AbstractRNG, x::BigInt, sp::SamplerBigInt)
31✔
387
    nlimbs = sp.nlimbs
31✔
388
    nlimbs == 0 && return MPZ.set!(x, sp.a)
31✔
389
    MPZ.realloc2!(x, sp.nlimbsmax*8*sizeof(Limb))
31✔
390
    @assert x.alloc >= nlimbs
31✔
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)
31✔
397
    GC.@preserve x begin
31✔
398
        limbs = UnsafeView(x.d, nlimbs-1)
31✔
399
        while true
31✔
400
            rand!(rng, limbs)
31✔
401
            hx = limbs[nlimbs] = rand(rng, sp.highsp)
31✔
402
            hx < hm && break # avoid calling mpn_cmp most of the time
31✔
403
            MPZ.mpn_cmp(x, sp.m, nlimbs) <= 0 && break
×
404
        end
×
405
        # adjust x.size (normally done by mpz_limbs_finish, in GMP version >= 6)
406
        while nlimbs > 0
31✔
407
            limbs[nlimbs] != 0 && break
31✔
408
            nlimbs -= 1
×
409
        end
×
410
        x.size = nlimbs
31✔
411
    end
412
    MPZ.add!(x, sp.a)
31✔
413
end
414

415

416
## random values from AbstractArray
417

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

421
rand(rng::AbstractRNG, sp::SamplerSimple{<:AbstractArray,<:Sampler}) =
1,959,499✔
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
218✔
428
    isempty(t) && throw(ArgumentError("collection must be non-empty"))
218✔
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)))
218✔
432
end
433

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

441
## random values from Set
442

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

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

448
## random values from BitSet
449

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

455
function rand(rng::AbstractRNG, sp::SamplerSimple{BitSet,<:Sampler})
2✔
456
    while true
4✔
457
        n = rand(rng, sp.data)
4✔
458
        n in sp[] && return n
4✔
459
    end
2✔
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) =
835✔
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}) =
×
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}) =
835✔
474
    SamplerTrivial(t)
475

476
function nth(iter, n::Integer)::eltype(iter)
835✔
477
    for (i, x) in enumerate(iter)
1,670✔
478
        i == n && return x
2,003✔
479
    end
1,168✔
480
end
481

482
rand(rng::AbstractRNG, sp::SamplerTrivial{<:Union{AbstractDict,AbstractSet}}) =
835✔
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,100✔
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}) =
×
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))
×
498
isvalid_unsafe(s::AbstractString, i) = isvalid(s, i)
×
499
_lastindex(s::String) = sizeof(s)
×
500
_lastindex(s::AbstractString) = lastindex(s)
×
501

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

510

511
## random elements from tuples
512

513
### 1
514

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

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

521
### 2
522

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

526
rand(rng::AbstractRNG, sp::SamplerSimple{Tuple{A,B}}) where {A,B} =
180✔
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} =
×
532
    SamplerSimple(t, Sampler(RNG, UInt52(), n))
533

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

543
### n
544

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

554
@generated function rand(rng::AbstractRNG, sp::SamplerSimple{T}) where T<:Tuple
×
555
    l = fieldcount(T)
×
556
    if l < typemax(UInt32) && ispow2(l)
×
557
        quote
×
558
            r = rand(rng, sp.data) & ($l-1)
×
559
            @inbounds return sp[][1 + r]
×
560
        end
561
    else
562
        :(@inbounds return sp[][rand(rng, sp.data)])
×
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