# Random numbers and threads

**URL:** <https://discourse.julialang.org/t/random-numbers-and-threads/77364>\
**Category:** General Usage\
**Tags:** question, multithreading, random\
**Created:** [March 3, 2022, 3:31pm UTC](https://discourse.julialang.org/t/random-numbers-and-threads/77364 "2022-03-03T15:31:24Z")\
**Posts on this page:** 1\
**Showing post:** 8

<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:** [March 3, 2022, 4:14pm UTC](https://discourse.julialang.org/t/random-numbers-and-threads/77364/8 "2022-03-03T16:14:46Z")

</div>

> [@lmiq](#):
>
> But what about the overhead associated to the concurrent access to the global random state

There isn’t a global random state. There’s a [per-task random state](https://github.com/JuliaLang/julia/blob/cbf6c1c8965aaba00683bfe4d54be9889368c1f6/stdlib/Random/src/Xoshiro.jl#L95-L114) used [by default](https://github.com/JuliaLang/julia/blob/82dc13005039a8215f1c910f48870ef378403cbc/stdlib/Random/src/RNGs.jl#L358-L359), if I understand correctly.

In general, now that [Julia 1.7 implemented task migration between threads](https://github.com/JuliaLang/julia/pull/40715), it is [problematic to use `threadid` to emulate thread-local state](https://github.com/JuliaLang/julia/pull/39168#issuecomment-757551959), and one should instead [use task-local state](https://docs.julialang.org/en/v1/base/parallel/#Base.task_local_storage-Tuple%7BAny%7D).

---

_[View the full topic](https://discourse.julialang.org/t/random-numbers-and-threads/77364)._
