Random number generators

Hi, I’m new to julia and have some questions about RNG’s.

If I understood well, MersenneTwister is the default method, but MersenneTwister objects are linked with the Random library and rand() function isn’t.

When using the rand() function without Random is there any limitation? Or it will still return [0,1) MersenneTwister random numbers.

Thanks :slight_smile:

If you look at the documentation for Random Numbers, the rand function has the following arguments and types

rand([rng=GLOBAL_RNG], [S], [dims...])

with the first argument being an AbstractRNG object. This means that you can actually call the rand function on any other type that inherits this AbstractRNG type. By doing this, you can effectively create multiple other RNG implementations, such is the case of the RandomNumbers.jl package.

Now, if you were to just call rand() out of the box, a GLOBAL_RNG is created which essentially creates a MersenneTwister type and uses that as the RNG. Thus it will return a random number from a Mersenne Twister implementation, as you have pointed out.

Finally, although it seems that rand is in Base and is separated from the Random module, actually the Random module is the one that is providing the implementation for the rand function, so they are actually one and the same. My guess is that rand is in Base for convenience, as it is used very frequently.

3 Likes

cf

2 Likes

It’s still the default, I suggested another RNG, and my understanding is the default will be changed, but hasn’t been for Julia 1.6. I also do recall a change to return (0,1] to avoid a very unlikely division-by-zero later when using the resulting number. I’m not sure that change is yet in.