Disable global RNG

If you intend to make the code multithreaded, it’s good practice to use the RNG version, since you’ll run into problems otherwise. See this post for an example of how to obtain an independent RNG per thread. (Also scroll up in the post to see the type of problems that can occur otherwise.)

As for disabling the global RNG, you could do something like this:

julia> rand(1, 4)
1×4 Array{Float64,2}:
 0.305565  0.252981  0.639368  0.759004

julia> Base.rand(dims::Integer...) = throw(ErrorException("Disabled. Use rand(rng, dims) instead."))

julia> rand(1, 4)
ERROR: Disabled. Use rand(rng, dims) instead.

(Note: There are other rand methods too that you’d need to redefine.) However, use this with care: it changes rand globally, so it won’t just affect your code.

4 Likes