# Distributions.jl using a custom random number generator

**URL:** <https://discourse.julialang.org/t/distributions-jl-using-a-custom-random-number-generator/5106>\
**Category:** Statistics\
**Created:** [July 28, 2017, 9:39am UTC](https://discourse.julialang.org/t/distributions-jl-using-a-custom-random-number-generator/5106 "2017-07-28T09:39:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 28, 2017, 9:39am UTC](https://discourse.julialang.org/t/distributions-jl-using-a-custom-random-number-generator/5106/1 "2017-07-28T09:39:32Z")

</div>

Is there any way to use a distribution from Distributions.jl with a custom RNG?

I know how to sample in a reproducible manner from a uniform distribution (see x1 and x2 in the code below).  
But Distrubutions.jl seems to rely on Base.Random.GLOBAL\_RNG which I do not want to use

I read this post, but was unable to solve my issue

> [@Performance issue with specifying random number generator](https://discourse.julialang.org/t/performance-issue-with-specifying-random-number-generator/1076/2):
>
> Ref: [https://github.com/JuliaLang/julia/issues/18488](https://github.com/JuliaLang/julia/issues/18488)

I found a Julia implementation for Binomial (in LightGraphs), which I might just use, but I would prefer to have this working for any distribution.

> <https://github.com/sbromberger/LightGraphs.jl/blob/1d4c341fdad3cfb6954361c107c7a5ca32903ae2/src/generators/randgraphs.jl#L41>

```julia
using RandomNumbers, Distributions

const RNG_XOR=Xorshifts.Xorshift1024Plus(0x1234567890abcdef)
const RNG_MERSENNE=MersenneTwister(2001)
const RNG_SELECTED=RNG_MERSENNE

x1=rand(RNG_XOR) #this works fine, x1 is reproducible each time I initialize my seed & custom RNG, I get the same value for x1
x2=rand(RNG_MERSENNE) #this works fine as well

# this line throws an error (I was hoping this would solve my problem...)
# Base.Random.GLOBAL_RNG=RNG_SELECTED

some_distr=Binomial(200,0.1)
rand(some_distr) #this 'value' is only reproducible if i run srand(some_seed) beforehand, but I want to use a different RNG

```

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [July 28, 2017, 7:56pm UTC](https://discourse.julialang.org/t/distributions-jl-using-a-custom-random-number-generator/5106/2 "2017-07-28T19:56:39Z")

</div>

As I understand it, this isn’t currently possible for all distributions. Distributions.jl uses Rmath for various complicated functions, and so Julia RNG objects can’t be passed to control random number generation.

If this isn’t the case I’d love to hear it. Sampling from `Distributions.jl` with a specified RNG is a much-desired feature for me, and I think the reliance on Rmath is a (totally understandable) wart in the Julia ecosystem.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 29, 2017, 6:12am UTC](https://discourse.julialang.org/t/distributions-jl-using-a-custom-random-number-generator/5106/3 "2017-07-29T06:12:22Z")

</div>

Thank you. I saw that RMath was called.  
Luckily Binomial, Bernoulli and Normal suffice for my use case (for now). Thus I can remain within Julia and rely on a custome RNG for those.
