# Cannot \`convert\` an object of type Random.\_GLOBAL\_RNG to an object of type Random.MersenneTwister

**URL:** <https://discourse.julialang.org/t/cannot-convert-an-object-of-type-random-global-rng-to-an-object-of-type-random-mersennetwister/41172>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 11, 2020, 12:29am UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type-random-global-rng-to-an-object-of-type-random-mersennetwister/41172 "2020-06-11T00:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![logankilpatrick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logankilpatrick/32/38123_2.png) [@logankilpatrick](https://discourse.julialang.org/u/logankilpatrick)\
**Post date:** [June 11, 2020, 12:29am UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type-random-global-rng-to-an-object-of-type-random-mersennetwister/41172/1 "2020-06-11T00:29:41Z")

</div>

Here is the error:

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Random._GLOBAL_RNG to an object of type Random.MersenneTwister
Closest candidates are:
  convert(::Type{S}, ::T) where {S, T<:CategoricalArrays.CategoricalValue} at /Users/logankilpatrick/.julia/packages/CategoricalArrays/nd8kj/src/value.jl:68
  convert(::Type{T}, ::T) where T at essentials.jl:171
  Random.MersenneTwister(::Any) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/Random/src/RNGs.jl:138
  ...
Stacktrace:

```

How would I define a convert function for this?

---

<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:** [June 11, 2020, 7:15am UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type-random-global-rng-to-an-object-of-type-random-mersennetwister/41172/2 "2020-06-11T07:15:00Z")

</div>

I think something like the following should be a sensible definition: `Base.convert(::Type{MersenneTwister}, rng::Random._GLOBAL_RNG) = copy(rng)`.

`Random._GLOBAL_RNG` is the type of `GLOBAL_RNG`, which is now kind of a “proxy” for the thread-local global RNG, which you get via `default_rng()` (before Julia 1.3, `GLOBAL_RNG` was an `MersenneTwister`). In the definition above, `copy(rng)` is specialized for `rng == GLOBAL_RNG`, and simply returns `copy(default_rng())`.

---

<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:** [June 12, 2020, 8:20am UTC](https://discourse.julialang.org/t/cannot-convert-an-object-of-type-random-global-rng-to-an-object-of-type-random-mersennetwister/41172/3 "2020-06-12T08:20:06Z")

</div>

> [@rfourquet](#):
>
> Base.convert(::Type{MersenneTwister}, rng::Random.\_GLOBAL\_RNG) = copy(rng)

That said, depending on the use-case, it might be enough to return `Random.default_rng()` without making a `copy`. Actually, it would be best that users don’t have to pirate-define this `convert` method, so it might be worthy opening a github issue.
