srand([rng], [seed])

Reseed the random number generator. If a seed is provided, the RNG will give a reproducible sequence of numbers, otherwise Julia will get entropy from the system. For MersenneTwister, the seed may be a non-negative integer, a vector of UInt32 integers or a filename, in which case the seed is read from a file. RandomDevice does not support seeding.


  1. Reseed the default random number generator:

    julia> srand(123)
    MersenneTwister(UInt32[0x0000007b], Random.DSFMT.DSFMT_state(Int32[578991598, 1073482918, 160911997, 1073181524, -1591984238, 1073033171, 8755, 1073033138, -32137, 1073033159  …  1073033196, 1, 0, -1, 0, 0, 0, 0, 0, 0]), [1.5])
    julia> rand()

    In this example, srand(123) seeds the default random number generator, MersenneTwister, with the seed value 123. Subsequent calls to rand() will produce the same sequence of random numbers.

  2. Reseed a specific random number generator:

    julia> rng = RandomDevice()
    julia> srand(rng, 42)
    MersenneTwister(UInt32[0x0000002a], Random.DSFMT.DSFMT_state(Int32[1348295911, 1073479697, 160910911, 1073078005, -1591984080, 1073033129, 8755, 1073033138, -32137, 1073033159  …  1073033196, 1, 0, -1, 0, 0, 0, 0, 0, 0]), [1.5])
    julia> rand(rng)

    Here, srand(rng, 42) reseeds a specific random number generator (rng in this case) with the seed value 42. Subsequent calls to rand(rng) will produce a reproducible sequence of random numbers using the specified generator.

Common mistake example:

julia> srand("random_seed.txt")
ERROR: MethodError: no method matching srand(::String)

In this example, the srand function is called with a string argument. However, srand does not accept a string as a seed. Make sure to provide a valid seed value, such as an integer or a vector of UInt32 integers, when using srand.

See Also

bitrand, MersenneTwister, rand, randcycle, randexp, randexp!, randjump, randn, randn!, RandomDevice, randperm, randsubseq, randsubseq!, shuffle, srand,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: