SeedRandom

SeedRandom[s]

resets the pseudorandom generator, using s as a seed.

SeedRandom[]

resets the generator, using as a seed the time of day and certain attributes of the current Wolfram System session.

Details and Options

  • You can use SeedRandom[s] to make sure you get the same sequence of pseudorandom numbers on different occasions.
  • SeedRandom affects RandomInteger, RandomReal, RandomComplex, RandomPrime, RandomChoice, RandomSample, and RandomVariate.
  • Possible settings for the seed s include:
  • RandomGeneratorState[]a RandomGeneratorState object
    nan integer
    "seed"a string
    Automaticautomatically choose whether to reseed based on options
  • SeedRandom returns a RandomGeneratorState that for deterministic generators can be used as a seed in order to reproduce random sequences.
  • SeedRandom[Method->"method"] specifies what method should be used for the pseudorandom generator. If "method" has been previously used, this form does not reseed it.
  • SeedRandom[n,Method->"method"] resets the generator, specifying both a seed and the method to use.
  • SeedRandom[n] affects only the current generator; SeedRandom[n,Method->All] affects all generators for which seeds have been given.
  • Typical possible methods include:
  • "Congruential"linear congruential generator (low-quality randomness)
    "ExtendedCA"extended cellular automaton generator (default)
    "OpenSSL"OpenSSL cryptographically secure generator
    "MersenneTwister"Mersenne twister shift register generator
    "MKL"Intel MKL generator (Intel-based systems)
    "Rule30CA"Wolfram Rule 30 generator
  • Different methods may give sequences with different levels of randomness.

Examples

open allclose all

Basic Examples  (1)

Use SeedRandom to make random numbers repeatable:

Scope  (3)

The seed can be a string:

Reseed the current random number generator:

Create a sequence of 5 random reals:

Reseed using the RandomGeneratorState object returned by SeedRandom:

The sequence of random real numbers is reproduced:

SeedRandom affects all random generators:

Options  (2)

Method  (2)

Switch to the Mersenne twister generator:

Random numbers are now generated using it:

A specific seed will affect the current Mersenne twister generator:

Give a seed to all initialized generators:

Applications  (2)

Reproduce a part of a computation that uses randomness:

Write a function that uses randomness, but returns predictable results:

Using the function again with more points just adds the additional points:

Properties & Relations  (4)

SeedRandom returns a RandomGeneratorState object with the new state:

When a RandomGeneratorState object is used as a seed, explicit Method options are ignored:

The state prior to seeding can be obtained from $RandomGeneratorState:

Use the current state:

Generate a new seed and and use the new state:

Seeding with the saved state reproduces the original sequence:

BlockRandom effectively saves the random generator state and restores it at the end of the computation:

Wolfram Research (1988), SeedRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/SeedRandom.html (updated 2020).

Text

Wolfram Research (1988), SeedRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/SeedRandom.html (updated 2020).

BibTeX

@misc{reference.wolfram_2021_seedrandom, author="Wolfram Research", title="{SeedRandom}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/SeedRandom.html}", note=[Accessed: 28-July-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_seedrandom, organization={Wolfram Research}, title={SeedRandom}, year={2020}, url={https://reference.wolfram.com/language/ref/SeedRandom.html}, note=[Accessed: 28-July-2021 ]}

CMS

Wolfram Language. 1988. "SeedRandom." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/SeedRandom.html.

APA

Wolfram Language. (1988). SeedRandom. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SeedRandom.html