# Randomness and reproducible results / unit tests

**URL:** https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941
**Category:** General Usage
**Tags:** reproducibility
**Created:** [March 11, 2021, 10:55am UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941 "2021-03-11T10:55:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 11, 2021, 10:55am UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/1 "2021-03-11T10:55:30Z")

</div>

I wonder if this is the suggested approach if one want reproducible results / set unit tests that don’t fail because of the random component, or other approaches are favoured…

```julia
using StableRNGs, Random

function myAlgorithm(;seed=nothing)
    rng = (seed==nothing) ? StableRNG(rand(1:typemax(Int64))) : StableRNG(seed)
    # My algorithm with random components that in some cases I want to test or be sure I got the some results every time
    # ...
    return rand(rng,4)
end

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [March 11, 2021, 11:11am UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/2 "2021-03-11T11:11:11Z")

</div>

There is this nice blog post by Tamas [How my Julia coding style changed](https://tamaspapp.eu/post/2019-09-07-coding_style_retrospective/), see section “Randomness”.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 11, 2021, 11:26am UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/3 "2021-03-11T11:26:15Z")

</div>

Changed titel from “ransomness” to “randomness” to ease my perfectionism

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [March 11, 2021, 12:17pm UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/4 "2021-03-11T12:17:26Z")

</div>

You can use functions in arguments, so it can be just

```julia
function myAlgorithm(; rng = StableRNG(rand(1:typemax(Int64))))

```

But since argument `rand` uses global random generator anyway, I prefer to use

```julia
function myAlgorithm(; rng = Random.GLOBAL_RNG)

```

Passing `rng` instead of `seed` is better, because it gives more flexibility.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [March 11, 2021, 12:34pm UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/5 "2021-03-11T12:34:26Z")

</div>

I see.

This is indeed how it works:

```julia
using StableRNGs, Random
const FIXEDSEED = 123
const FIXEDRNG = StableRNG(FIXEDSEED)

function myAlgorithm(;seed=nothing)
    rng = (seed==nothing) ? StableRNG(rand(1:typemax(Int64))) : StableRNG(seed)
    return rand(rng,4)
end
function myAlgorithm2(;rng = Random.GLOBAL_RNG)
    return rand(rng,4)
end

myAlgorithm(seed=123) # always the same result
myAlgorithm2(rng=FIXEDRNG) # always the same sequence of results on each run of the script ("pulling" from the same rng object on different calls)
myAlgorithm2(rng=StableRNG(FIXEDSEED)) # always the same result (new rng object on each call)

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [March 11, 2021, 12:40pm UTC](https://discourse.julialang.org/t/randomness-and-reproducible-results-unit-tests/56941/6 "2021-03-11T12:40:07Z")

</div>

To expand on flexibility: you can use [RandomNumbers.jl](https://github.com/sunoru/RandomNumbers.jl) package, and choose best generator for your needs. Some could be slower but produce better random streams, some can be faster. There are counter based random generators and they are used in [parallel computations](https://juliafolds.github.io/FoldsCUDA.jl/dev/examples/monte_carlo_pi/). So, using generator instead of a seed, gives much more than just reproducibility. I achieved +20% speedup once, just by using different generator instead of the default `MersenneTwister`.
