WOLFRAM

BlockRandom[expr]

evaluates expr with all pseudorandom generators localized, so that uses of SeedRandom, RandomInteger, and related functions within the evaluation of expr do not affect subsequent pseudorandom sequences.

Details and Options

  • BlockRandom[expr] in effect saves the states of all pseudorandom generators before evaluating expr, then restores them afterwards.
  • BlockRandom[expr] always gives the same result as just evaluating expr, though it can affect values obtained for subsequent expressions.
  • BlockRandom[expr,RandomSeeding->seeding] specifies that the random generators should be seeded with the specified seeding before evaluating expr.
  • BlockRandom has attribute HoldFirst.
  • Many built-in functions in the Wolfram Language effectively use BlockRandom.

Examples

open allclose all

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

Use BlockRandom to localize the use of a pseudorandom generator:

Out[1]=1

Use BlockRandom to prevent one use of a randomized algorithm from affecting others:

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

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

BlockRandom works with all pseudorandom generators in the Wolfram Language:

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

Options  (3)Common values & functionality for each option

RandomSeeding  (3)

Use a particular random seed to ensure predictable results:

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

When a generator is used outside BlockRandom, the results without seeding are changed:

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

Use a RandomGeneratorState object to simultaneously specify the seed and method:

Out[1]=1

Use Automatic seeding to get different results every time:

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

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

Run a randomized algorithm without affecting other random sequences:

Out[1]=1

Compute a Monte Carlo approximation to :

Out[3]=3

BlockRandom prevents other random sequences from being affected:

Out[4]=4

Get the "same" random number at different precisions:

Out[1]=1

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

BlockRandom effectively does the same as resetting the random seed to its previous value:

Out[1]=1
Out[2]=2
Wolfram Research (2007), BlockRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/BlockRandom.html (updated 2017).
Wolfram Research (2007), BlockRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/BlockRandom.html (updated 2017).

Text

Wolfram Research (2007), BlockRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/BlockRandom.html (updated 2017).

Wolfram Research (2007), BlockRandom, Wolfram Language function, https://reference.wolfram.com/language/ref/BlockRandom.html (updated 2017).

CMS

Wolfram Language. 2007. "BlockRandom." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/BlockRandom.html.

Wolfram Language. 2007. "BlockRandom." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/BlockRandom.html.

APA

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

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

BibTeX

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

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

BibLaTeX

@online{reference.wolfram_2025_blockrandom, organization={Wolfram Research}, title={BlockRandom}, year={2017}, url={https://reference.wolfram.com/language/ref/BlockRandom.html}, note=[Accessed: 04-April-2025 ]}

@online{reference.wolfram_2025_blockrandom, organization={Wolfram Research}, title={BlockRandom}, year={2017}, url={https://reference.wolfram.com/language/ref/BlockRandom.html}, note=[Accessed: 04-April-2025 ]}