@@ -23,17 +23,17 @@ Sampler(rng::AbstractRNG, ::Type{T}, n::Repetition) where {T<:AbstractFloat} =
2323# generic random generation function which can be used by RNG implementors
2424# it is not defined as a fallback rand method as this could create ambiguities
2525
26- rand_generic (r:: AbstractRNG , :: CloseOpen{Float16} ) =
26+ rand (r:: AbstractRNG , :: SamplerTrivial{ CloseOpen{Float16} } ) =
2727 Float16 (reinterpret (Float32,
28- (rand_ui10_raw (r) % UInt32 << 13 ) & 0x007fe000 | 0x3f800000 ) - 1 )
28+ (rand (r, UInt10 ( UInt32)) << 13 ) | 0x3f800000 ) - 1 )
2929
30- rand_generic (r:: AbstractRNG , :: CloseOpen{Float32} ) =
31- reinterpret (Float32, rand_ui23_raw (r) % UInt32 & 0x007fffff | 0x3f800000 ) - 1
30+ rand (r:: AbstractRNG , :: SamplerTrivial{ CloseOpen{Float32} } ) =
31+ reinterpret (Float32, rand (r, UInt23 ()) | 0x3f800000 ) - 1
3232
33- rand_generic (r:: AbstractRNG , :: Close1Open2_64 ) =
34- reinterpret (Float64, 0x3ff0000000000000 | rand (r, UInt64) & 0x000fffffffffffff )
33+ rand (r:: AbstractRNG , :: SamplerTrivial{ Close1Open2_64} ) =
34+ reinterpret (Float64, 0x3ff0000000000000 | rand (r, UInt52 ()) )
3535
36- rand_generic (r:: AbstractRNG , :: CloseOpen_64 ) = rand (r, Close1Open2 ()) - 1.0
36+ rand (r:: AbstractRNG , :: SamplerTrivial{ CloseOpen_64} ) = rand (r, Close1Open2 ()) - 1.0
3737
3838# ### BigFloat
3939
@@ -101,11 +101,21 @@ rand(rng::AbstractRNG, sp::SamplerBigFloat{T}) where {T<:FloatInterval{BigFloat}
101101
102102# ## random integers
103103
104- rand_ui10_raw (r:: AbstractRNG ) = rand (r, UInt16)
105- rand_ui23_raw (r:: AbstractRNG ) = rand (r, UInt32)
104+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt10Raw{UInt16}} ) = rand (r, UInt16)
105+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt23Raw{UInt32}} ) = rand (r, UInt32)
106106
107- rand_ui52_raw (r:: AbstractRNG ) = reinterpret (UInt64, rand (r, Close1Open2 ()))
108- rand_ui52 (r:: AbstractRNG ) = rand_ui52_raw (r) & 0x000fffffffffffff
107+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt52Raw{UInt64}} ) =
108+ _rand52 (r, rng_native_52 (r))
109+
110+ _rand52 (r:: AbstractRNG , :: Type{Float64} ) = reinterpret (UInt64, rand (r, Close1Open2 ()))
111+ _rand52 (r:: AbstractRNG , :: Type{UInt64} ) = rand (r, UInt64)
112+
113+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt10{UInt16}} ) = rand (r, UInt10Raw ()) & 0x03ff
114+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt23{UInt32}} ) = rand (r, UInt23Raw ()) & 0x007fffff
115+ rand (r:: AbstractRNG , :: SamplerTrivial{UInt52{UInt64}} ) = rand (r, UInt52Raw ()) & 0x000fffffffffffff
116+
117+ rand (r:: AbstractRNG , sp:: SamplerTrivial{<:UniformBits{T}} ) where {T} =
118+ rand (r, uint_default (sp[])) % T
109119
110120# ## random complex numbers
111121
@@ -158,25 +168,28 @@ function SamplerRangeFast(r::AbstractUnitRange{T}) where T<:Union{Int128,UInt128
158168 SamplerRangeFast {UInt128,T} (first (r), bw, m, mask)
159169end
160170
161- function rand_lteq (r:: AbstractRNG , randfun , u:: U , mask:: U ) where U<: Integer
171+ function rand_lteq (r:: AbstractRNG , S , u:: U , mask:: U ) where U<: Integer
162172 while true
163- x = randfun (r ) & mask
173+ x = rand (r, S ) & mask
164174 x <= u && return x
165175 end
166176end
167177
178+ # helper function, to turn types to values, should be removed once we can do rand(Uniform(UInt))
179+ rand (rng:: AbstractRNG , :: Val{T} ) where {T} = rand (rng, T)
180+
168181function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt64,T} ) where T
169182 a, bw, m, mask = sp. a, sp. bw, sp. m, sp. mask
170- x = bw <= 52 ? rand_lteq (rng, rand_ui52_raw , m, mask) :
171- rand_lteq (rng, rng -> rand (rng, UInt64), m, mask)
183+ x = bw <= 52 ? rand_lteq (rng, UInt52Raw () , m, mask) :
184+ rand_lteq (rng, Val ( UInt64), m, mask)
172185 (x + a % UInt64) % T
173186end
174187
175188function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt128,T} ) where T
176189 a, bw, m, mask = sp. a, sp. bw, sp. m, sp. mask
177- x = bw <= 52 ? rand_lteq (rng, rand_ui52_raw, m % UInt64 , mask % UInt64) % UInt128 :
178- bw <= 104 ? rand_lteq (rng, rand_ui104_raw , m, mask) :
179- rand_lteq (rng, rng -> rand (rng, UInt128), m, mask)
190+ x = bw <= 52 ? rand_lteq (rng, UInt52Raw () , mask % UInt64) % UInt128 :
191+ bw <= 104 ? rand_lteq (rng, UInt104Raw () , m, mask) :
192+ rand_lteq (rng, Val ( UInt128), m, mask)
180193 x % T + a
181194end
182195
0 commit comments