149
149
150
150
# ### helper functions
151
151
152
- function rand_lteq (r:: AbstractRNG , S, u:: U , mask:: U ) where U<: Integer
153
- while true
154
- x = rand (r, S) & mask
155
- x <= u && return x
156
- end
157
- end
158
-
159
- function rand_lteq (rng:: AbstractRNG , S, u:: T ):: T where T
160
- while true
161
- x = rand (rng, S)
162
- x <= u && return x
163
- end
164
- end
165
-
166
- # helper function, to turn types to values, should be removed once we
167
- # can do rand(Uniform(UInt))
168
- rand (rng:: AbstractRNG , :: Val{T} ) where {T} = rand (rng, T)
169
-
170
152
uint_sup (:: Type{<:Union{Bool,BitInteger}} ) = UInt32
171
153
uint_sup (:: Type{<:Union{Int64,UInt64}} ) = UInt64
172
154
uint_sup (:: Type{<:Union{Int128,UInt128}} ) = UInt128
@@ -193,22 +175,24 @@ end
193
175
194
176
function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt32,T} ) where T
195
177
a, bw, m, mask = sp. a, sp. bw, sp. m, sp. mask
196
- x = rand_lteq (rng, Val (UInt32), m, mask)
178
+ x = rand (rng, LessThan ( m, Masked ( mask, uniform (UInt32))) )
197
179
(x + a % UInt32) % T
198
180
end
199
181
200
182
function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt64,T} ) where T
201
183
a, bw, m, mask = sp. a, sp. bw, sp. m, sp. mask
202
- x = bw <= 52 ? rand_lteq (rng, UInt52Raw (), m, mask) :
203
- rand_lteq (rng, Val (UInt64), m, mask)
184
+ x = bw <= 52 ? rand (rng, LessThan ( m, Masked ( mask, UInt52Raw ())) ) :
185
+ rand (rng, LessThan ( m, Masked ( mask, uniform (UInt64))) )
204
186
(x + a % UInt64) % T
205
187
end
206
188
207
189
function rand (rng:: AbstractRNG , sp:: SamplerRangeFast{UInt128,T} ) where T
208
190
a, bw, m, mask = sp. a, sp. bw, sp. m, sp. mask
209
- x = bw <= 52 ? rand_lteq (rng, UInt52Raw (), m % UInt64, mask % UInt64) % UInt128 :
210
- bw <= 104 ? rand_lteq (rng, UInt104Raw (), m, mask) :
211
- rand_lteq (rng, Val (UInt128), m, mask)
191
+ x = bw <= 52 ?
192
+ rand (rng, LessThan (m % UInt64, Masked (mask % UInt64, UInt52Raw ()))) % UInt128 :
193
+ bw <= 104 ?
194
+ rand (rng, LessThan (m, Masked (mask, UInt104Raw ()))) :
195
+ rand (rng, LessThan (m, Masked (mask, uniform (UInt128))))
212
196
x % T + a
213
197
end
214
198
@@ -266,19 +250,20 @@ Sampler(::AbstractRNG, r::AbstractUnitRange{T},
266
250
:: Repetition ) where {T<: Union{Bool,BitInteger} } = SamplerRangeInt (r)
267
251
268
252
rand (rng:: AbstractRNG , sp:: SamplerRangeInt{T,UInt32} ) where {T<: Union{Bool,BitInteger} } =
269
- (unsigned (sp. a) + rem_knuth (rand_lteq (rng, Val (UInt32), sp. u), sp. k)) % T
253
+ (unsigned (sp. a) + rem_knuth (rand (rng, LessThan (sp. u, uniform (UInt32))), sp. k)) % T
254
+
270
255
271
256
# this function uses 52 bit entropy for small ranges of length <= 2^52
272
257
function rand (rng:: AbstractRNG , sp:: SamplerRangeInt{T,UInt64} ) where T<: BitInteger
273
- x = sp. bw <= 52 ? rand_lteq (rng, UInt52 (), sp. u) :
274
- rand_lteq (rng, Val (UInt64), sp. u)
258
+ x = sp. bw <= 52 ? rand (rng, LessThan ( sp. u, UInt52 ()) ) :
259
+ rand (rng, LessThan ( sp. u, uniform (UInt64)) )
275
260
return ((sp. a % UInt64) + rem_knuth (x, sp. k)) % T
276
261
end
277
262
278
263
function rand (rng:: AbstractRNG , sp:: SamplerRangeInt{T,UInt128} ) where T<: BitInteger
279
- x = sp. bw <= 52 ? rand_lteq (rng, UInt52 (UInt128), sp. u) :
280
- sp. bw <= 104 ? rand_lteq (rng, UInt104 (UInt128), sp. u) :
281
- rand_lteq (rng, Val (UInt128), sp. u)
264
+ x = sp. bw <= 52 ? rand (rng, LessThan ( sp. u, UInt52 (UInt128)) ) :
265
+ sp. bw <= 104 ? rand (rng, LessThan ( sp. u, UInt104 (UInt128)) ) :
266
+ rand (rng, LessThan ( sp. u, uniform (UInt128)) )
282
267
return ((sp. a % UInt128) + rem_knuth (x, sp. k)) % T
283
268
end
284
269
0 commit comments