# Performance issue with specifying random number generator

**URL:** <https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076>\
**Category:** Internals & Design\
**Created:** [December 20, 2016, 11:05pm UTC](https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076 "2016-12-20T23:05:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [December 20, 2016, 11:05pm UTC](https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076/1 "2016-12-20T23:05:59Z")

</div>

I like the ability to specify the random number generator (RNG) in functions like rand().

This feature would be much more useful if we could tell rand() to use the default RNG, rather than one we supply. This could be achieved either by:

1. Exposing the name of the default RNG (that is, the one we seed with srand()), or
2. Making the RNG a keyword argument.

This problem arises because I have some pieces of code that I sometimes want to run with the system RNG and sometimes without. Right now, I can not find an elegant way to do this, and have to either use a branch or duplicate a lot of code.

I originally tried to resolve the problem by having my code create a new RNG when one is not specified. However, this was happening so often that the creation of the MersenneTwister became the dominant cost of the computation.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 20, 2016, 11:16pm UTC](https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076/2 "2016-12-20T23:16:11Z")

</div>

> [@danspielman](#):
>
> Exposing the name of the default RNG

Ref:

> <https://github.com/JuliaLang/julia/issues/18488>
>
> http://docs.julialang.org/en/latest/stdlib/numbers/?highlight=rand#Base.rand ref…ers to GLOBAL\_RNG, but the value is not exported.
> 
> My use case is I write a random number generator (specialized for my use), and the underlying AbstractRNG is passed in to the constructor, with GLOBAL\_RNG as the default value.
> 
> Shouldn't GLOBAL\_RNG be exported?

---

<div class="post-metadata">

**Author:** ![viralbshah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viralbshah/32/54_2.png) [@viralbshah](https://discourse.julialang.org/u/viralbshah)\
**Post date:** [December 21, 2016, 8:36am UTC](https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076/3 "2016-12-21T08:36:15Z")

</div>

For now, Base.Random.GLOBAL\_RNG should work to pass as AbstractRNG for the default system RNG. The issue above discusses making it simpler.
