Rand() vs rand(rng)

Hello.

Why is rand(rng) ten times slower than rand() ?

See code below:

julia> using Random

julia> rng = MersenneTwister(12345);

julia> function test()
t0=time()
x=0.0;
for k=1:100000000
x+=rand(rng)
end
x,time()-t0
end
test (generic function with 1 method)

julia> test()
(4.999454956309856e7, 8.787218809127808)


julia> function test()
t0=time()
x=0.0;
for k=1:100000000
x+=rand()
end
x,time()-t0
end
test (generic function with 1 method)

julia> test()
(5.000040368583618e7, 0.8501379489898682)

Try const rng = MersenneTwister(12345); That said, as of 1.6 one reason this would be slower is that the RNG is switching to xoshiro256 which is much faster than a MersenneTwister

4 Likes

“RNG is switching to xoroshiro which is much faster than a MersenneTwister”

Couple of follow up questions:

  1. is it possible to seed xoroshiro (so that I get the same random sequence each time I run my simulation) ?

  2. how does the period (cycle length) of xoroshiro compare to Mersenne Twister ?

Thank you.

didn’t find the PR can you post a link?

https://github.com/JuliaLang/julia/pull/34852 is the PR. It is still seedable (API change would be breaking). The period is 2^256 (so infinite for all useful purposes).

2 Likes