Yes, that. It is not completely evident that those index transformations are more expensive than a call to rand()
though. (unfortunately to get the indexes right I would spend all day…). But a simple guess suggests that it might be something of the same order:
julia> function iter(n,draw)
for i in 1:1000
draw(n)
end
nothing
end
iter (generic function with 1 method)
julia> function draw2(n)
a = rand(1:n^2 - n)
b = mod(a,n) + div(n^3,n) # <--- some random stuff
if b >= a
b += 1
end
a, b
end
draw2 (generic function with 1 method)
julia> @btime iter(3,$draw2)
13.623 μs (0 allocations: 0 bytes)
julia> function draw3(n)
a = rand(1:n)
b = rand(1:n-1)
if b >= a
b += 1
end
a, b
end
draw2 (generic function with 1 method)
julia> @btime iter(3,$draw3)
14.473 μs (0 allocations: 0 bytes)