Skip to content

Commit 2d10bd7

Browse files
committed
slightly faster version
1 parent b6d022e commit 2d10bd7

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

stdlib/Random/src/generation.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,21 @@ function rand(rng::AbstractRNG, sp::SamplerRangeNDL{U,T}) where {U,T}
375375
s = sp.s
376376
x = widen(rand(rng, U))
377377
m = x * s
378-
l = m % U
379-
if l < s
380-
rand_unlikely(rng, sp, x, m, l)
381-
else
382-
(s == 0 ? x : m >> (8*sizeof(U))) % T + sp.a
383-
end
378+
r = (m % U) < s ? rand_unlikely(rng, s, m) % T :
379+
iszero(s) ? x % T :
380+
(m >> (8*sizeof(U))) % T
381+
r + sp.a
384382
end
385383

386384
# similar to `randn_unlikely` : splitting out this unlikely path leads to faster code
387-
@noinline function rand_unlikely(rng::AbstractRNG, sp::SamplerRangeNDL{U,T}, x, m, l) where {U, T}
388-
s = sp.s
385+
@noinline function rand_unlikely(rng, s::U, m)::U where {U}
386+
# m needs to be passed, because the while loop below might have no iterations
389387
t = mod(-s, s) # as s is unsigned, -s is equal to 2^L - s in the paper
390-
while l < t
388+
while (m % U) < t
391389
x = widen(rand(rng, U))
392390
m = x * s
393-
l = m % U
394391
end
395-
# s != 0
396-
m >> (8*sizeof(U)) % T + sp.a
392+
(m >> (8*sizeof(U))) % U
397393
end
398394

399395

0 commit comments

Comments
 (0)