# Why doesn't Random.seed! allocate for StableRNGs.LehmerRNG?

**URL:** https://discourse.julialang.org/t/why-doesnt-random-seed-allocate-for-stablerngs-lehmerrng/130621
**Category:** General Usage
**Tags:** random
**Created:** [July 10, 2025, 8:46pm UTC](https://discourse.julialang.org/t/why-doesnt-random-seed-allocate-for-stablerngs-lehmerrng/130621 "2025-07-10T20:46:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![RoyW](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@RoyW](https://discourse.julialang.org/u/RoyW)
#### Post date: [July 10, 2025, 8:46pm UTC](https://discourse.julialang.org/t/why-doesnt-random-seed-allocate-for-stablerngs-lehmerrng/130621/1 "2025-07-10T20:46:03Z")

</div>

I ran the following code on my machine:

```julia
import StableRNGs
using BenchmarkTools, Random

const T = Float64

rng = StableRNGs.LehmerRNG(0)
rng2 = Random.Xoshiro(0)
rng3 = Random.MersenneTwister(0)

@btime randn($rng, T)
@btime randn($rng2, T)
@btime randn($rng3, T)

@btime Random.seed!($rng, 123)
@btime Random.seed!($rng2, 123)
@btime Random.seed!($rng3, 123)

```

My REPL shows:

```julia
2.420 ns (0 allocations: 0 bytes)
2.440 ns (0 allocations: 0 bytes)
3.300 ns (0 allocations: 0 bytes)
2.060 ns (0 allocations: 0 bytes)
291.890 ns (7 allocations: 368 bytes)
6.358 μs (9 allocations: 424 bytes)

```

The documentation for StableRNGs.jl is that it focuses on stable streams. Is it an unintended benefit for `StableRNGs.LehmerRNG` to not allocate when used with `Random.seed!`?

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [July 20, 2025, 9:36am UTC](https://discourse.julialang.org/t/why-doesnt-random-seed-allocate-for-stablerngs-lehmerrng/130621/2 "2025-07-20T09:36:09Z")

</div>

`seed!` doesn’t allocate with `StableRNG` because the algorithm is simpler (and arguably [too simple](https://github.com/JuliaRandom/StableRNGs.jl/issues/9)); it essentially just uses the seed as is to initialize the RNG state.

With `Xoshiro` and `MersenneTwister`, the seed is hashed (currently with SHA256) before initializing the RNG state, and this takes time and allocations. This gives confidence that close seeds give uncorrelated RNGs (like `Xoshiro(1)` and `Xoshiro(2)`).
