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.
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 all close allBasic Examples (2)
Use BlockRandom to localize the use of a pseudorandom generator:
{RandomReal[], BlockRandom[RandomReal[]], RandomReal[], RandomReal[]}Use BlockRandom to prevent one use of a randomized algorithm from affecting others:
Table[Total[RandomInteger[1, 100]], {20}]Table[BlockRandom[Total[RandomInteger[1, 100]]], {20}]Scope (1)
BlockRandom works with all pseudorandom generators in the Wolfram Language:
Table[BlockRandom[RandomReal[1, 2]], {3}]Table[BlockRandom[RandomChoice[{a, b, c}, 5]], {3}]Options (3)
RandomSeeding (3)
Use a particular random seed to ensure predictable results:
Table[BlockRandom[RandomInteger[9, 10]], {2}]Table[BlockRandom[RandomInteger[9, 10], RandomSeeding -> 1234], {2}]When a generator is used outside BlockRandom, the results without seeding are changed:
RandomInteger[9, 10]Table[BlockRandom[RandomInteger[9, 10]], {2}]Table[BlockRandom[RandomInteger[9, 10], RandomSeeding -> 1234], {2}]Use a RandomGeneratorState object to simultaneously specify the seed and method:
BlockRandom[RandomReal[1, 5], RandomSeeding -> RandomGeneratorState[{"ExtendedCA", {80, 4, 0}},
{{NumericArray[{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "UnsignedInteger64"],
0, 20, 20}, {RawArray["UnsignedInteger64", {8402553920425805686, 9934563772124633544,
13 ... 91188148046, 1589259630949761215,
10047890358231901395, 5864488619226313396, 17324547311914965925, 9027643525916071240,
2022314520320920354, 9575528438213517111}], 4, 0}}, NumericArray[{6155169294560358013, 0},
"UnsignedInteger64"]]]Use Automatic seeding to get different results every time:
Table[BlockRandom[RandomInteger[9, 10]], {4}]Table[BlockRandom[RandomInteger[9, 10], RandomSeeding -> Automatic], {4}]Applications (2)
Run a randomized algorithm without affecting other random sequences:
SeedRandom[1234];RandomReal[1, 5]SeedRandom[1234];Compute a Monte Carlo approximation to
:
BlockRandom[4.0 Mean[Table[Boole[Norm[RandomReal[1, 2]] < 1], {10 ^ 4}]]]BlockRandom prevents other random sequences from being affected:
RandomReal[1, 5]Get the "same" random number at different precisions:
Table[BlockRandom[RandomReal[WorkingPrecision -> p]], {p, 10}]Properties & Relations (1)
BlockRandom effectively does the same as resetting the random seed to its previous value:
{SeedRandom[123];RandomReal[], SeedRandom[123];RandomReal[]}{SeedRandom[123];BlockRandom[RandomReal[]], RandomReal[]}Tech Notes
Related Guides
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_2026_blockrandom, author="Wolfram Research", title="{BlockRandom}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/BlockRandom.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_blockrandom, organization={Wolfram Research}, title={BlockRandom}, year={2017}, url={https://reference.wolfram.com/language/ref/BlockRandom.html}, note=[Accessed: 12-June-2026]}