Dealing with seeds in random numbers

Dear users,
Anyone could say me how can I generate random numbers in Julia by using a given seed?
I accept examples.
I need to generate some data for a paper, and then provide the seed for other researches to use the same ones.
Thank you very much.

You do it by creating a “random number generator” (RNG) object (see the Random docs):

julia> using Random

julia> rng = MersenneTwister(12345);  # 12345 is the seed

julia> rand(rng, 10)

I need to generate some data for a paper, and then provide the seed for other researches to use the same ones.

I would strongly urge you to simply post the data. (Many journals allow you to attach data files as “supplementary information”, and various fields also have public data repositories.)

As discussed e.g. here, Julia is unlikely to guarantee indefinitely that pseudorandom streams always produce exactly the same sequence of numbers from a given seed. Numpy recently came to the same conclusion.

Of course, you could also supply the exact version of Julia to use and a package manifest specifying the exact versions of all packages. Personally, I feel like this is the wrong way to approach reproducibility — decades from now, people should be able to reproduce your results without having to resurrect ancient languages and software versions (and maybe ancient hardware emulators!). The data files and algorithms should be archived in a manner that allows the results to be reproduced (at least up to small differences in rounding errors) with any language and set of libraries.

6 Likes

Thank you so much for your detailed response!

Do you think it would make sense to write up something similar (but shorter) in the manual as the protocol we follow for RNG changes? Eg

  1. random streams are not guaranteed to be reproducible when the minor or major version of Julia changes,
  2. but patch version changes should not affect the random stream,
  3. old RNGs are kept available in some library.
2 Likes