BlockRandom

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)

Use BlockRandom to localize the use of a pseudorandom generator:

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

Scope  (1)

BlockRandom works with all pseudorandom generators in the Wolfram Language:

Options  (3)

RandomSeeding  (3)

Use a particular random seed to ensure predictable results:

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

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

Use Automatic seeding to get different results every time:

Applications  (2)

Run a randomized algorithm without affecting other random sequences:

Compute a Monte Carlo approximation to :

BlockRandom prevents other random sequences from being affected:

Get the "same" random number at different precisions:

Properties & Relations  (1)

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

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).

CMS

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

BibTeX

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

BibLaTeX

@online{reference.wolfram_2024_blockrandom, organization={Wolfram Research}, title={BlockRandom}, year={2017}, url={https://reference.wolfram.com/language/ref/BlockRandom.html}, note=[Accessed: 22-November-2024 ]}