@@ -23,17 +23,17 @@ Sampler(rng::AbstractRNG, ::Type{T}, n::Repetition) where {T<:AbstractFloat} =
23
23
# generic random generation function which can be used by RNG implementors
24
24
# it is not defined as a fallback rand method as this could create ambiguities
25
25
26
- rand_generic (r:: AbstractRNG , :: CloseOpen{Float16} ) =
26
+ rand (r:: AbstractRNG , :: SamplerTrivial{ CloseOpen{Float16} } ) =
27
27
Float16 (reinterpret (Float32,
28
- (rand_ui10_raw (r) % UInt32 << 13 ) & 0x007fe000 | 0x3f800000 ) - 1 )
28
+ (rand (r, UInt10 ( UInt32)) << 13 ) | 0x3f800000 ) - 1 )
29
29
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
32
32
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 ()) )
35
35
36
- rand_generic (r:: AbstractRNG , :: CloseOpen_64 ) = rand (r, Close1Open2 ()) - 1.0
36
+ rand (r:: AbstractRNG , :: SamplerTrivial{ CloseOpen_64} ) = rand (r, Close1Open2 ()) - 1.0
37
37
38
38
# ### BigFloat
39
39
@@ -101,11 +101,21 @@ rand(rng::AbstractRNG, sp::SamplerBigFloat{T}) where {T<:FloatInterval{BigFloat}
101
101
102
102
# ## random integers
103
103
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)
106
106
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
109
119
110
120
# ## random complex numbers
111
121
@@ -158,25 +168,28 @@ function SamplerRangeFast(r::AbstractUnitRange{T}) where T<:Union{Int128,UInt128
158
168
SamplerRangeFast {UInt128,T} (first (r), bw, m, mask)
159
169
end
160
170
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
162
172
while true
163
- x = randfun (r ) & mask
173
+ x = rand (r, S ) & mask
164
174
x <= u && return x
165
175
end
166
176
end
167
177
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
+
168
181
function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt64,T} ) where T
169
182
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)
172
185
(x + a % UInt64) % T
173
186
end
174
187
175
188
function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt128,T} ) where T
176
189
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 () , m % UInt64, mask % UInt64) % UInt128 :
191
+ bw <= 104 ? rand_lteq (rng, UInt104Raw () , m, mask) :
192
+ rand_lteq (rng, Val ( UInt128), m, mask)
180
193
x % T + a
181
194
end
182
195
0 commit comments