# Add Random.seed!(0) with block to standard library

**URL:** <https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908>\
**Category:** Internals & Design\
**Created:** [October 6, 2025, 6:56am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908 "2025-10-06T06:56:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [October 6, 2025, 6:56am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/1 "2025-10-06T06:56:45Z")

</div>

Useful in writing deterministic tests

```julia-auto
@testset "SkewT fit_mle" begin
  Random.seed!(0) do
    x = rand(SkewT(1.0, 2.0, 5.0, 0.5), 10_000);
    (; μ, σ, ν, λ) = fit_mle(SkewT, x)
    @test [μ, σ, ν, λ] ≈ [1.0, 2.0, 5.0, 0.5] rtol=0.05
  end
end

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 6, 2025, 7:40am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/2 "2025-10-06T07:40:53Z")

</div>

That’s already in Julia v1.12 for testsets: [www.julialang.org/blog/2025/10/julia-1.12-highlights.md at b2f7dd210cc400cf29a2bb5525644f995286c947 · JuliaLang/www.julialang.org · GitHub](https://github.com/JuliaLang/www.julialang.org/blob/b2f7dd210cc400cf29a2bb5525644f995286c947/blog/2025/10/julia-1.12-highlights.md#reproducing-rng-state-in-testsets)

---

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [October 6, 2025, 9:05am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/3 "2025-10-06T09:05:18Z")

</div>

If I understand correctly - the doc you mentioned say if test is non deterministic, it would print the random number state if test failed, to reproduce it.

Wile this change makes the test deterministic.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 6, 2025, 9:13am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/4 "2025-10-06T09:13:08Z")

</div>

You missed the bit where you can pass a custom RNG to `@testset`

> Normally, it’d require several attempts to reproduce a similar failure, but now the RNG is printed to screen and you can reproduce the run in a new session by setting the `rng` option of `@testset` to the value printed in the failed test:
> 
> ```julia-repl
> julia> using Test, Random
> 
> julia> @testset rng=Random.Xoshiro(0xd02e9404e1026b37, 0xca5ae9c15acf6752, 0x976a327d42433534, 0xb5b1305af1734f3a, 0x1c2aa037d6e7d5c7) begin
> @test rand() > 0.001
> end;
> test set: Test Failed at REPL[2]:2
> Expression: rand() > 0.001
> Evaluated: 0.00036328334842516963 > 0.001
> 
> Stacktrace:
> [1] top-level scope
> @ REPL[2]:2
> [2] macro expansion
> @ ~/.julia/juliaup/julia-1.12.0.x64.linux.gnu/share/julia/stdlib/v1.12/Test/src/Test.jl:1776 [inlined]
> [3] macro expansion
> @ REPL[2]:2 [inlined]
> [4] macro expansion
> @ ~/.julia/juliaup/julia-1.12.0.x64.linux.gnu/share/julia/stdlib/v1.12/Test/src/Test.jl:680 [inlined]
> Test Summary: | Fail Total Time
> test set | 1 1 1.4s
> RNG of the outermost testset: Xoshiro(0xd02e9404e1026b37, 0xca5ae9c15acf6752, 0x976a327d42433534, 0xb5b1305af1734f3a, 0x1c2aa037d6e7d5c7)
> ERROR: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 6, 2025, 9:32am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/5 "2025-10-06T09:32:52Z")

</div>

Also note that hardcoding an RNG like that is not a good practice, and should be reserved for reproducing an existing failure. As the [RNG docs](https://docs.julialang.org/en/v1/stdlib/Random/#Random-Numbers) say:

> Relying on a specific seed or generated stream of numbers during unit testing is thus discouraged - consider testing properties of the methods in question instead.

Hardcoding certain results can and does hide issues in your code, and is not guaranteed to be stable.

Shameless plug, consider using property based testing libraries like Supposition.jl for this.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 6, 2025, 9:35am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/6 "2025-10-06T09:35:56Z")

</div>

> [@Sukera](#):
>
> Also note that hardcoding an RNG like that is not a good practice, and should be reserved for reproducing an existing failure.

Yes, the way I envisioned the `rng` option for `@testset` was only to reproduce observed failures which only depend on the global RNG, because that happens a lot and reproducing failures observed on remote machines can be frustratingly hard (especially because `@testset` does know the RNG state but doesn’t tell you 😡). But if someone for some reason wants to have fully deterministic runs they should consider passing the RNG object explicitly to `rand` and friends (which is also faster than accessing the global RNG, BTW), or even better test statistical properties of distributions rather than exact numbers, as you also suggested.

---

<div class="post-metadata">

**Author:** ![Alex1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex1/32/11771_2.png) [@Alex1](https://discourse.julialang.org/u/Alex1)\
**Post date:** [October 6, 2025, 9:48am UTC](https://discourse.julialang.org/t/add-random-seed-0-with-block-to-standard-library/132908/7 "2025-10-06T09:48:38Z")

</div>

Ok, feel free to close the issue.

P.S. I think the tests should be deterministic, predictable and reproducible.
