WARNING: Base.MersenneTwister is deprecated: it has been moved to the standard library package `Random`

I got this warning. Then, I did “using Random” and also “import Random”. But it did not work still. Can anyone please let me know what’s going on? I’m using Julia version 0.7.

julia> using Random

julia> rand(MersenneTwister(0), Dict(1=>2, 3=>4))
WARNING: Base.MersenneTwister is deprecated: it has been moved to the standard library package `Random`.
Add `using Random` to your imports.
 in module Main
1 => 2

julia> rng=MersenneTwister(1234)
WARNING: Base.MersenneTwister is deprecated: it has been moved to the standard library package `Random`.
Add `using Random` to your imports.
 in module Main
Random.MersenneTwister(UInt32[0x000004d2], Random.DSFMT.DSFMT_state(Int32[-1393240018, 1073611148, 45497681, 1072875908, 436273599, 1073674613, -2043716458, 1073445557, -254908435, 1072827086  …  -599655111, 1073144102, 367655457, 1072985259, -1278750689, 1018350124, -597141475, 249849711, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000  …  0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)

julia> import Random

julia> rng=MersenneTwister(1234)
WARNING: Base.MersenneTwister is deprecated: it has been moved to the standard library package `Random`.
Add `using Random` to your imports.
 in module Main
Random.MersenneTwister(UInt32[0x000004d2], Random.DSFMT.DSFMT_state(Int32[-1393240018, 1073611148, 45497681, 1072875908, 436273599, 1073674613, -2043716458, 1073445557, -254908435, 1072827086  …  -599655111, 1073144102, 367655457, 1072985259, -1278750689, 1018350124, -597141475, 249849711, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000  …  0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)

Is that from a fresh Julia session? If not, try restarting Julia, then do using Random before you try to access MersenneTwister

1 Like

@ssfrr, great. it works this time. By the way, is there a uniform way/function call in julia 0.7 to generate a random number based on a given random number generator and a given probability distribution?

You can set a seed before generating the random numbers. For example with

using Random
Random.seed!(1234)

@IljaK91, thank you so much for your quick reply. But the following is what I meant.

julia> using Distributions, Random

julia> rng = MersenneTwister();

julia> dist = Normal(0.2, 0.01);

julia> rand(rng, dist)
0.2116549672938699

That’s what you mean?

@carstenbauer, thank you. I think so. Will dig more about it.

@carstenbauer, not good enough though.

julia> dist=Categorical([0.57,0.245,0.185])
Categorical{Float64}(K=3, p=[0.57, 0.245, 0.185])

julia> rand(rng,dist)
ERROR: ArgumentError: Sampler for this object is not defined
Stacktrace:
 [1] Random.Sampler(::Type{MersenneTwister}, ::Random.SamplerTrivial{Categorical{Float64},Any}, ::Val{1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v0.7/Random/src/Random.jl:134
 [2] Random.Sampler(::MersenneTwister, ::Random.SamplerTrivial{Categorical{Float64},Any}, ::Val{1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v0.7/Random/src/Random.jl:131
 [3] rand(::MersenneTwister, ::Random.SamplerTrivial{Categorical{Float64},Any}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v0.7/Random/src/Random.jl:221 (repeats 2 times)
 [4] top-level scope at none:0

That seems strange to me, since rand(Categorical([0.57,0.245,0.185])) works fine. Maybe a bug/missing method?

@carstenbauer, I do not think it’s a bug. rand function for the categorical distribution only use the default global random number generator. It does not support self-defined random number generator.

Also, a small note - you seem to be pasting your code samples as a quote. It’ll display more nicely if you wrap it in triple-backticks, e.g.

```
<your code>
```

edit: (Whoops, that was in response to @bsnyh, not @carstenbauer)

1 Like