# Best practices to ensure determinstic behaviour when random numbers are used

**URL:** https://discourse.julialang.org/t/best-practices-to-ensure-determinstic-behaviour-when-random-numbers-are-used/138527
**Category:** General Usage
**Created:** [July 29, 2026, 3:13pm UTC](https://discourse.julialang.org/t/best-practices-to-ensure-determinstic-behaviour-when-random-numbers-are-used/138527 "2026-07-29T15:13:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)
#### Post date: [July 29, 2026, 3:13pm UTC](https://discourse.julialang.org/t/best-practices-to-ensure-determinstic-behaviour-when-random-numbers-are-used/138527/1 "2026-07-29T15:13:26Z")

</div>

Hi!  
I have been playing around with some number theory algorithms like Rabin-Shallit’s algorithm (see [LagrangeFourSquares.jl](https://github.com/HMegh/LagrangeFourSquares.jl)) for finding a four-square representation (i.e., given n find a,b,c,d such that n=a^2+b^2+c^2+d^2). However, this tasks has two issues:

- There could be many solutions. For example, 28=1^2+1^2+1^2+5^2=1^2+3^2+3^2+3^2.
- The algorithm that I am working with uses a random number generator to obtain some intermediary guesses.

So my concern is that this function is not deterministic. I am wondering if other Julians had a similar issue and how they dealt with it. Right now, I basically initialize an RNG with the same seed whenever the function is called to keep it deterministic. Is that what is usually done?

* * *

The closest analogy I can think of is finding a divisor of n by randomly testing numbers below \sqrt{n}:

```julia-auto
using Random

f(n)=begin
    rng=MersenneTwister(1234)
    isqrtn=isqrt(n)
    while true 
        m=rand(rng,2:isqrtn)
        mod(n,m)==0 && return m
    end
end

```

This appears to be deterministic but if the RNG was updated, it would break tests like `f(10)==2`.

---

<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: [July 29, 2026, 3:18pm UTC](https://discourse.julialang.org/t/best-practices-to-ensure-determinstic-behaviour-when-random-numbers-are-used/138527/2 "2026-07-29T15:18:07Z")

</div>

StableRNGs.jl may be useful

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 29, 2026, 3:55pm UTC](https://discourse.julialang.org/t/best-practices-to-ensure-determinstic-behaviour-when-random-numbers-are-used/138527/3 "2026-07-29T15:55:59Z")

</div>

> [@HMegh](#):
>
> So my concern is that this function is not deterministic. I am wondering if other Julians had a similar issue and how they dealt with it. Right now, I basically initialize an RNG with the same seed whenever the function is called to keep it deterministic. Is that what is usually done?

See the discussion of exactly this topic in the manual: [Random Numbers: Reproducibility](https://docs.julialang.org/en/v1/stdlib/Random/#Reproducibility)

If you really want a deterministic algorithm, of course, then one could question whether you should be using pseudo-random numbers at all, as opposed to a [low-discrepancy sequence](https://en.wikipedia.org/wiki/Low-discrepancy_sequence) (for a [Quasi Monte-Carlo algorithm](https://en.wikipedia.org/wiki/Quasi-Monte_Carlo_method)), e.g. using Sobol.jl.
