WOLFRAM

gives a representation of the internal state of a pseudorandom generator.

Details

  • RandomGeneratorState[] contains the generator method, method parameters and, when applicable, the full internal state.
  • A RandomGeneratorState object can be given to SeedRandom to restore the state it represents.
  • Some methods have a state that can only persist for the duration of the kernel session in which it was created.
  • The following properties can be extracted using RandomGeneratorState[]["prop"]:
  • "CrossSessionPersistent"whether the internal state persists past the current kernel session
    "Method"method name
    "StateHash"the hash of the state as an integer

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Get the current state of the pseudorandom generator:

Out[1]=1

Use a RandomGeneratorState object to seed BlockRandom:

Out[1]=1

Scope  (5)Survey of the scope of standard use cases

Seed the current random generator and save the resulting state:

Out[1]=1

A particular sequence of numbers is generated:

Out[2]=2

Reseed using the RandomGeneratorState object:

Out[3]=3

The same sequence of numbers is generated:

Out[4]=4

Some methods have states that can only be restored in the same session:

Out[1]=1

SeedRandom will work as expected in the same kernel session:

Out[2]=2

Trying to restore these states in a new kernel session will restore the method but not the state:

Out[3]=3

Some methods do not have a restorable state:

Out[1]=1
Out[2]=2

SeedRandom can use these to set the method, but random sequences will not be reproduced:

Out[3]=3
Out[4]=4

Programmatically extract the method name:

Out[1]=1

Programmatically determine whether the state survives the end of the kernel session:

Out[1]=1
Out[2]=2

Applications  (1)Sample problems that can be solved with this function

Create a version of BlockRandom that refuses to run if the current random state cannot be restored:

Activate a randomness generation method with no restorable state:

Out[3]=3

Because this method cannot be seeded, BlockRandom cannot give reproducible results:

Out[4]=4

The function will refuse to evaluate:

Out[5]=5

Activate a randomness generation method that possesses a restorable state:

Out[6]=6

The function restores the random state at the end of each evaluation:

Out[7]=7

Hence the following result matches the previous result:

Out[8]=8

Properties & Relations  (5)Properties of the function, and connections to other functions

When available, the state hash is returned as an integer:

Out[1]=1

If the state hash has expired, it is returned as None:

Out[1]=1

The current random generator state is stored in $RandomGeneratorState:

Out[1]=1

SeedRandom returns a RandomGeneratorState object:

Out[1]=1
Out[2]=2

A RandomGeneratorState object can be used as a seed in SeedRandom and related functions:

Use state in SeedRandom:

Out[2]=2

Use it to seed computations in BlockRandom:

Out[3]=3

Possible Issues  (1)Common pitfalls and unexpected behavior

When a session-only RandomGeneratorState object is given to SeedRandom, a copy is created:

Out[1]=1

This copy has a different session hash:

Out[2]=2

However, both RandomGeneratorState objects produce the same random sequences:

Out[3]=3
Wolfram Research (2020), RandomGeneratorState, Wolfram Language function, https://reference.wolfram.com/language/ref/RandomGeneratorState.html.
Wolfram Research (2020), RandomGeneratorState, Wolfram Language function, https://reference.wolfram.com/language/ref/RandomGeneratorState.html.

Text

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

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

CMS

Wolfram Language. 2020. "RandomGeneratorState." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/RandomGeneratorState.html.

Wolfram Language. 2020. "RandomGeneratorState." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/RandomGeneratorState.html.

APA

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

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

BibTeX

@misc{reference.wolfram_2025_randomgeneratorstate, author="Wolfram Research", title="{RandomGeneratorState}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/RandomGeneratorState.html}", note=[Accessed: 06-April-2025 ]}

@misc{reference.wolfram_2025_randomgeneratorstate, author="Wolfram Research", title="{RandomGeneratorState}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/RandomGeneratorState.html}", note=[Accessed: 06-April-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_randomgeneratorstate, organization={Wolfram Research}, title={RandomGeneratorState}, year={2020}, url={https://reference.wolfram.com/language/ref/RandomGeneratorState.html}, note=[Accessed: 06-April-2025 ]}

@online{reference.wolfram_2025_randomgeneratorstate, organization={Wolfram Research}, title={RandomGeneratorState}, year={2020}, url={https://reference.wolfram.com/language/ref/RandomGeneratorState.html}, note=[Accessed: 06-April-2025 ]}