# How to set seed for Random.TaskLocalRNG()?

**URL:** <https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236>\
**Category:** Machine Learning\
**Created:** [January 24, 2026, 4:26am UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236 "2026-01-24T04:26:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 24, 2026, 4:26am UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/1 "2026-01-24T04:26:00Z")

</div>

I see the following:

```julia-auto
using MLJ
Tree = @load DecisionTreeClassifier pkg=DecisionTree

julia> tree = Tree()
DecisionTreeClassifier(
  max_depth = -1,
  min_samples_leaf = 1,
  min_samples_split = 2,
  min_purity_increase = 0.0,
  n_subfeatures = 0,
  post_prune = false,
  merge_purity_threshold = 1.0,
  display_depth = 5,
  feature_importance = :impurity,
  rng = Random.TaskLocalRNG())

```

not much success after reading docs about `Random.TaskLocalRNG`. How could I set a fixed seed for it? Should I use

```julia-auto
Random.seed!(123)

```

or

```julia-auto
Random.seed!(Random.TaskLocalRNG(), 123)

```

?

thanks

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [January 24, 2026, 6:12am UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/2 "2026-01-24T06:12:25Z")

</div>

I usually use `Random.Xoshiro(seed)` for this purpose. Does `Tree(rng=Random.Xoshiro(42))` work?

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 24, 2026, 11:02am UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/3 "2026-01-24T11:02:59Z")

</div>

from the docs, seems like `TaskLocalRNG` has some distinct advantages like being reproducible in multithread environment. That’s why it’s chosen as the default.

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [January 24, 2026, 2:57pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/4 "2026-01-24T14:57:49Z")

</div>

According to `?Random.seed!`:

> After the call to `seed!`, `rng` is equivalent to a newly  
> created object initialized with the same seed.

So even though there’s “no method matching `Random.TaskLocalRNG(::Int64)`” (which I find bizarre), you can indeed use `Random.seed!`:

```julia
julia> rng = Random.seed!(Random.TaskLocalRNG(), 123)
Random.TaskLocalRNG()

julia> randn(rng)
-0.6457306721039767

julia> rng = Random.seed!(Random.TaskLocalRNG(), 123);

julia> randn(rng)
-0.6457306721039767

julia> let
        rng = Random.TaskLocalRNG()
        rng_new = Random.seed!(rng)
        rng === rng_new
       end
true

```

From the same docstring:

> If `rng` is not specified, it defaults to seeding the state of the shared  
> task-local generator.

`Random.seed!(123)` seeds some default, existing RNG returned by `Random.default_rng`, while `Random.seed!(Random.TaskLocalRNG(), 123)` creates a new one.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [January 24, 2026, 3:18pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/5 "2026-01-24T15:18:41Z")

</div>

That’s not right. There is only one task local RNG per task, so `Random.seed!(Random.TaskLocalRNG(), 123)` does not create a new RNG. It seeds the existing one. That’s also why there isn’t a `Random.TaskLocalRNG(::Int64)` method - you can’t make a new task local RNG.

`seed!(x::Int)` is equivalent to `seed!(Random.TaskLocalRNG(), x::Int)`, except that the former also stores the used seed, such that it can be ‘reset’ later by functionality such as `@testset`.

The `TaskLocalRNG` is a zero-sized struct that refers to four of the five `rngState` fields in the existing task:

```julia
julia> dump(Task)
mutable struct Task <: Any
 [... more fields ...]
  rngState0::UInt64
  rngState1::UInt64
  rngState2::UInt64
  rngState3::UInt64
  rngState4::UInt64
 [... more fields ...]

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 24, 2026, 3:31pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/6 "2026-01-24T15:31:23Z")

</div>

thanks for the help.

after some `@enter`, I think the correct seeding should be `Random.seed!(Random.TaskLocalRNG(), 123)`, but _ **not** _ `Random.seed!(123)`.  
I got this conclusion by:

```julia-auto
julia> @enter Random.seed!(123)
 427 function seed!(seed=nothing)
>428 seed!(default_rng(), seed)

```

which is seeding `default_rng()`.

now

```julia-auto
julia> @enter Random.seed!(Random.TaskLocalRNG(), 123)
>259 seed!(rng::Union{TaskLocalRNG, Xoshiro}, seed) =
 260 initstate!(rng, reinterpret(UInt64, hash_seed(seed)))
...
 236 @inline function initstate!(x::Union{TaskLocalRNG, Xoshiro}, state)
>237 length(state) == 4 && eltype(state) == UInt64 ||
 238 throw(ArgumentError("initstate! expects a list of 4 `UInt64` values"))
 239 s0, s1, s2, s3 = state
 240 setstate!(x, (s0, s1, s2, s3, 1s0 + 3s1 + 5s2 + 7s3))
...
 213 @inline function setstate!(x::TaskLocalRNG, (s0, s1, s2, s3, s4))
>214 t = current_task()
 215 t.rngState0 = s0
 216 t.rngState1 = s1
 217 t.rngState2 = s2
 218 t.rngState3 = s3

```

is actually seeding `TaskLocalRNG`.

am I correct?

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [January 24, 2026, 3:52pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/7 "2026-01-24T15:52:43Z")

</div>

The default RNG is the task local RNg (although that may change in the future)

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 24, 2026, 4:43pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/8 "2026-01-24T16:43:55Z")

</div>

indeed…

```julia-auto
julia> Random.default_rng()
Random.TaskLocalRNG()

```

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [January 24, 2026, 5:13pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/9 "2026-01-24T17:13:20Z")

</div>

> [@jakobnissen](#):
>
> you can’t make a new task local RNG

TIL. Apparently `Random.TaskLocalRNG()` always returns the same object:

```julia
julia> let
        r1 = Random.TaskLocalRNG()
        r2 = Random.TaskLocalRNG()
        r1 === r2
       end
true

```

So the object returned by `Random.TaskLocalRNG()` is always the same. That’s the first time I see a constructor that doesn’t return a new object…

As a side note, why not make `Random.TaskLocalRNG(seed::Int) = Random.seed!(Random.TaskLocalRNG(), seed)`? That would make the API equivalent to, say, `Xoshiro` and `MersenneTwister`.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [January 24, 2026, 5:16pm UTC](https://discourse.julialang.org/t/how-to-set-seed-for-random-tasklocalrng/135236/10 "2026-01-24T17:16:43Z")

</div>

There are lots of constructors returning the same object! Consider:

```julia
julia> Nothing() === Nothing()
true

julia> Missing() === Missing()
true

julia> EOFError() === EOFError()
true

julia> Base.HasLength() === Base.HasLength()
true

```

This happens whenever a type has no runtime information (e.g. it’s just an immutable struct with no fields)
