Do you have a general rule of thumb for picking one over the other?
Benchmarks are inconsistent depending on how many random numbers are generated:
julia> rng1 = MersenneTwister(123)
MersenneTwister(123)
julia> rng2 = Xoshiro(123)
Xoshiro(0xfefa8d41b8f5dca5, 0xf80cc98e147960c1, 0x20e2ccc17662fc1d, 0xea7a7dcb2e787c01, 0xf4e85a418b9c4f80)
julia> @btime rand($rng1, 1);
27.570 ns (2 allocations: 64 bytes)
julia> @btime rand($rng2, 1);
19.695 ns (2 allocations: 64 bytes)
julia> @btime rand($rng1, 100);
118.183 ns (2 allocations: 928 bytes)
julia> @btime rand($rng2, 100);
133.110 ns (2 allocations: 928 bytes)
I just always use Xoshiro. MT is kinda just for backwards compatibility
That is what I was told. I am curious why it is slower in the n=100 case above.
FWIW, I don't see Xoshiro lose at any size on my machine:
julia> using Random
julia> rng1 = MersenneTwister(123)
MersenneTwister(123)
julia> rng2 = Xoshiro(123)
Xoshiro(0xfefa8d41b8f5dca5, 0xf80cc98e147960c1, 0x20e2ccc17662fc1d, 0xea7a7dcb2e787c01, 0xf4e85a418b9c4f80)
julia> @btime rand($rng1, 1);
20.245 ns (2 allocations: 64 bytes)
julia> @btime rand($rng2, 1);
15.435 ns (2 allocations: 64 bytes)
julia> @btime rand($rng1, 100);
71.995 ns (2 allocations: 928 bytes)
julia> @btime rand($rng2, 100);
66.703 ns (2 allocations: 928 bytes)
julia> @btime rand($rng1, 10000);
3.859 μs (3 allocations: 78.20 KiB)
julia> @btime rand($rng2, 10000);
2.639 μs (3 allocations: 78.20 KiB)
I wouldn't be surprised if this sort of thing was pretty architecture dependent.
Interesting. I will move to Xoshiro in that case.
Júlio Hoffimann has marked this topic as resolved.
IIRC, MT buffers a bunch of random numbers which xoshiro doesn't. So when you time with @btime, which gives the minimum time, you don't benchmark the actual time to generate the numbers, only that to copy it from the buffer
Thanks for sharing.
There's a whole funny rivalry between the Xoshiro and PCG people, but Mersenne is old and either is probably an upgrade over that IMHO
Last updated: Jul 22 2025 at 04:56 UTC