({{, (RefLink[BlockRandom, paclet:ref/BlockRandom])[expr] 
evaluates expr with all pseudorandom generators localized, so that uses of (RefLink[SeedRandom, paclet:ref/SeedRandom]), (RefLink[RandomInteger, paclet:ref/RandomInteger]), and related functions within the evaluation of expr do not affect subsequent pseudorandom sequences.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[BlockRandom, paclet:ref/BlockRandom])[expr] in effect saves the states of all pseudorandom generators before evaluating expr, then restores them afterwards.

(RefLink[BlockRandom, paclet:ref/BlockRandom])[expr] always gives the same result as just evaluating expr, though it can affect values obtained for subsequent expressions.

(RefLink[BlockRandom, paclet:ref/BlockRandom])[expr,(RefLink[RandomSeeding, paclet:ref/RandomSeeding])->seeding] specifies that the random generators should be seeded with the specified seeding before evaluating expr.

(RefLink[BlockRandom, paclet:ref/BlockRandom]) has attribute (RefLink[HoldFirst, paclet:ref/HoldFirst]).

Many built-in functions in the Wolfram Language effectively use (RefLink[BlockRandom, paclet:ref/BlockRandom]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Use (RefLink[BlockRandom, paclet:ref/BlockRandom]) to localize the use of a pseudorandom generator:

{RandomReal[],BlockRandom[RandomReal[]],RandomReal[],RandomReal[]}

Use (RefLink[BlockRandom, paclet:ref/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}]

OpenersExampleSection1(Spacer1)Scope  (1)

(RefLink[BlockRandom, paclet:ref/BlockRandom]) works with all pseudorandom generators in the Wolfram Language:

Table[BlockRandom[RandomReal[1,2]],{3}]

Table[BlockRandom[RandomChoice[{a,b,c},5]],{3}]

OpenersExampleSection2(Spacer1)Options  (2)

OpenersExampleSubsection0(Spacer1)RandomSeeding  (2)

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 (RefLink[BlockRandom, paclet:ref/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 (RefLink[Automatic, paclet:ref/Automatic]) seeding to get different results every time:

Table[BlockRandom[RandomInteger[9,10]],{4}]

Table[BlockRandom[RandomInteger[9,10],RandomSeeding->Automatic],{4}]

OpenersExampleSection3(Spacer1)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}]]]

(RefLink[BlockRandom, paclet:ref/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}]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[BlockRandom, paclet:ref/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[]}

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SeedRandom, paclet:ref/SeedRandom])  ▪  (RefLink[RandomSeeding, paclet:ref/RandomSeeding])  ▪  (RefLink[RandomInteger, paclet:ref/RandomInteger])  ▪  (RefLink[RandomReal, paclet:ref/RandomReal])  ▪  (RefLink[RandomChoice, paclet:ref/RandomChoice])  ▪  (RefLink[RandomSample, paclet:ref/RandomSample])  ▪  (RefLink[RandomEntity, paclet:ref/RandomEntity])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Pseudorandom Numbers, paclet:tutorial/PseudorandomNumbers])}, {▪(RefLinkPlain[Random Number Generation, paclet:tutorial/RandomNumberGenerationOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Package Bulletproofing, paclet:guide/PackageBulletproofing])}, {▪(RefLinkPlain[Random Number Generation, paclet:guide/RandomNumberGeneration])}, {▪(RefLinkPlain[Tuning & Debugging, paclet:guide/TuningAndDebugging])}, {▪(RefLinkPlain[Scoping Constructs, paclet:guide/ScopingConstructs])}})}})