Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Procedural Programming > Scoping Constructs > BlockRandom >
Mathematica > Core Language > Package Development > Namespace Management > Scoping Constructs > BlockRandom >

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.
  • 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.
  • Many built-in functions in Mathematica effectively use BlockRandom.
Use BlockRandom to localize the use of a pseudorandom generator:
Use BlockRandom to prevent one use of a randomized algorithm from affecting others:
Use BlockRandom to localize the use of a pseudorandom generator:
In[1]:=
Click for copyable input
Out[1]=
 
Use BlockRandom to prevent one use of a randomized algorithm from affecting others:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
BlockRandom works with all pseudorandom generators in Mathematica:
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:
BlockRandom effectively does the same as resetting the random seed to its previous value:
New in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF