# Rand() vs rand(rng)

**URL:** https://discourse.julialang.org/t/rand-vs-rand-rng/46517
**Category:** Performance
**Created:** [September 12, 2020, 7:01pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517 "2020-09-12T19:01:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![steve1313](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@steve1313](https://discourse.julialang.org/u/steve1313)
#### Post date: [September 12, 2020, 7:01pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517/1 "2020-09-12T19:01:54Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 12, 2020, 7:06pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517/2 "2020-09-12T19:06:38Z")

</div>

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`

---

<div class="post-metadata">

### Author: ![steve1313](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@steve1313](https://discourse.julialang.org/u/steve1313)
#### Post date: [September 12, 2020, 7:10pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517/3 "2020-09-12T19:10:42Z")

</div>

“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.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 12, 2020, 7:17pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517/4 "2020-09-12T19:17:04Z")

</div>

> [@Oscar\_Smith](#):
>
> RNG is switching to xoroshiro

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

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [September 12, 2020, 7:20pm UTC](https://discourse.julialang.org/t/rand-vs-rand-rng/46517/5 "2020-09-12T19:20:22Z")

</div>

[https://github.com/JuliaLang/julia/pull/34852](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).
