Pseudorandom Numbers

The Wolfram Language has three functions for generating pseudorandom numbers that are distributed uniformly over a range of values.

Pseudorandom number generation.

Generating tables of pseudorandom numbers.

This will give 0 or 1 with equal probability:

RandomInteger[]

This gives a pseudorandom complex number:

RandomComplex[]

This gives a list of 10 pseudorandom integers between 0 and 9 (inclusive):

RandomInteger[{0,9}, 10]

This gives a matrix of pseudorandom reals between 0 and 1:

RandomReal[1, {3,3}]

(RefLink[RandomReal, paclet:ref/RandomReal]) and (RefLink[RandomComplex, paclet:ref/RandomComplex]) allow you to obtain pseudorandom numbers with any precision.

Changing the precision for pseudorandom numbers.

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

RandomReal[WorkingPrecision->30]

Here is a list of four 20-digit pseudorandom complex numbers:

RandomComplex[{-1-I, 1 + I}, 4, WorkingPrecision->20]

If you get arrays of pseudorandom numbers 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:

Sqrt[x^2]==Abs[x]

Substituting in a random numerical value shows that the equation is not always (RefLink[True, paclet:ref/True]):

%/.x->RandomComplex[]

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

(RefLink[RandomInteger, paclet:ref/RandomInteger]), (RefLink[RandomReal, paclet:ref/RandomReal]), and (RefLink[RandomComplex, paclet:ref/RandomComplex]) are unlike almost any other Wolfram Language functions in that every time you call them, you potentially get a different result. If you use them in a calculation, therefore, you may get different answers on different occasions.

The sequences that you get from (RefLink[RandomInteger, paclet:ref/RandomInteger]), (RefLink[RandomReal, paclet:ref/RandomReal]), and (RefLink[RandomComplex, paclet:ref/RandomComplex]) 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 the Wolfram Language 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 Wolfram Language 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 (RefLink[SeedRandom, paclet:ref/SeedRandom]).

Pseudorandom number generator seed.

This reseeds the pseudorandom generator:

SeedRandom[143]

Here are three pseudorandom numbers:

RandomReal[1,{3}]

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

SeedRandom[143];RandomReal[1,{3}]

Every single time (RefLink[RandomInteger, paclet:ref/RandomInteger]), (RefLink[RandomReal, paclet:ref/RandomReal]), or (RefLink[RandomComplex, paclet:ref/RandomComplex]) is called, the internal state of the pseudorandom generator that it uses is changed. This means that subsequent calls to these functions made in subsidiary calculations will have an effect on the numbers returned in your main calculation. To avoid any problems associated with this, you can localize this effect of their use by doing the calculation inside of (RefLink[BlockRandom, paclet:ref/BlockRandom]).

Localizing the effects of using (RefLink[RandomInteger, paclet:ref/RandomInteger]), (RefLink[RandomReal, paclet:ref/RandomReal]), or (RefLink[RandomComplex, paclet:ref/RandomComplex]).

By localizing the calculation inside (RefLink[BlockRandom, paclet:ref/BlockRandom]), the internal state of the pseudorandom generator is restored after generating the first list:

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

Many applications require random numbers from non-uniform distributions. The Wolfram Language has many distributions built into the system. You can give a distribution with appropriate parameters instead of a range to (RefLink[RandomInteger, paclet:ref/RandomInteger]) or (RefLink[RandomReal, paclet:ref/RandomReal]).

Generating pseudorandom numbers with non-uniform distributions.

This generates 12 integers distributed by the Poisson distribution with mean 3:

RandomInteger[PoissonDistribution[3], 12]

This generates a 4×4 matrix of real numbers using the standard normal distribution:

RandomReal[NormalDistribution[], {4,4}]

This generates five high-precision real numbers distributed normally with mean 2 and standard deviation 4:

RandomReal[NormalDistribution[2,4], 5, WorkingPrecision->32]

An additional use of pseudorandom numbers is for selecting from a list. (RefLink[RandomChoice, paclet:ref/RandomChoice]) selects with replacement and (RefLink[RandomSample, paclet:ref/RandomSample]) samples without replacement.

Selecting at random.

Choose 10 items at random from the digits 0 through 9:

RandomChoice[Range[0,9], 10]

Chances are very high that at least one of the choices was repeated in the output. That is because when an element is chosen, it is immediately replaced. On the other hand, if you want to select from an actual set of elements, there should be no replacement.

Sample 10 items at random from the digits 0 through 9 without replacement. The result is a random permutation of the digits:

RandomSample[Range[0,9], 10]

Sample 10 items from a set having different frequencies for each digit:

RandomSample[{0,1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9},10]