This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Numerical FunctionsInteger and Number-Theoretical Functions

3.2.3 Pseudorandom Numbers

Pseudorandom number generation.

This gives a list of 3 pseudorandom numbers.

In[1]:= Table[Random[ ], {3}]


Here is a 30-digit pseudorandom real number in the range 0 to 1.

In[2]:= Random[Real, {0, 1}, 30]


This gives a list of 8 pseudorandom integers between 100 and 200 (inclusive).

In[3]:= Table[Random[Integer, {100, 200}], {8}]


If you call Random[ ] repeatedly, you should get a "typical" sequence of numbers, with no particular pattern. There are many ways to use such numbers.

One common way to use pseudorandom numbers is in making numerical tests of hypotheses. For example, if you believe that two symbolic expressions are mathematically equal, you can test this by plugging in "typical" numerical values for symbolic parameters, and then comparing the numerical results. (If you do this, you should be careful about numerical accuracy problems and about functions of complex variables that may not have unique values.)

Here is a symbolic equation.

In[4]:= Sin[Cos[x]] == Cos[Sin[x]]


Substituting in a random numerical value shows that the equation is not always True.

In[5]:= % /. x -> Random[ ]


Other common uses of pseudorandom numbers include simulating probabilistic processes, and sampling large spaces of possibilities. The pseudorandom numbers that Mathematica generates are always uniformly distributed over the range you specify.

Random is unlike almost any other Mathematica function in that every time you call it, you potentially get a different result. If you use Random in a calculation, therefore, you may get different answers on different occasions.

The sequences that you get from Random[ ] are not in most senses "truly random", although they should be "random enough" for practical purposes. The sequences are in fact produced by applying a definite mathematical algorithm, starting from a particular "seed". If you give the same seed, then you get the same sequence.

When Mathematica starts up, it takes the time of day (measured in small fractions of a second) as the seed for the pseudorandom number generator. Two different Mathematica sessions will therefore almost always give different sequences of pseudorandom numbers.

If you want to make sure that you always get the same sequence of pseudorandom numbers, you can explicitly give a seed for the pseudorandom generator, using SeedRandom.

This reseeds the pseudorandom generator.

In[6]:= SeedRandom[143]

Here are three pseudorandom numbers.

In[7]:= Table[Random[ ], {3}]


If you reseed the pseudorandom generator with the same seed, you get the same sequence of pseudorandom numbers.

In[8]:= SeedRandom[143]; Table[Random[ ], {3}]


Every single time Random is called, the internal state of the pseudorandom generator that it uses is changed. This means that calls to Random made in subsidiary calculations will have an effect on the numbers returned by Random in your main calculation. To avoid any problems associated with this, you can save the value of $RandomState before you do subsidiary calculations, and then restore it afterwards.

By localizing the value of $RandomState using Block, the internal state of the pseudorandom generator is restored after generating the first list.

In[9]:= {Block[{$RandomState}, {Random[ ], Random[ ]}],

{Random[ ], Random[ ]}}


Numerical FunctionsInteger and Number-Theoretical Functions