# Pseudorandom Numbers

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

RandomInteger[] | 0 or 1 with probability |

RandomInteger[{i_{min},i_{max}}] | an integer between and , inclusive |

RandomInteger[i_{max}] | an integer between 0 and , inclusive |

RandomReal[ ] | a real number between 0 and 1 |

RandomReal[{, }] | a real number between and |

RandomReal[] | a real number between 0 and |

RandomComplex[] | a complex number in the unit square |

RandomComplex[{z_{min},z_{max}}] | a complex number in the rectangle defined by and |

RandomComplex[] | a complex number in the rectangle defined by 0 and |

Pseudorandom number generation.

RandomReal[range,n],RandomComplex[range,n],RandomInteger[range,n] | |

a list of n pseudorandom numbers from the given range | |

RandomReal[range,{n_{1},n_{2},…}],RandomComplex[range,{n_{1},n_{2},…}],RandomInteger[range,{n_{1},n_{2},…}] | |

an ××… array of pseudorandom numbers |

Generating tables of pseudorandom numbers.

RandomReal and RandomComplex allow you to obtain pseudorandom numbers with any precision.

option name | default value | |

WorkingPrecision | MachinePrecision | precision to use for real or complex numbers |

Changing the precision for pseudorandom numbers.

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.)

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.

RandomInteger, RandomReal, and 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 RandomInteger, RandomReal, and 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 SeedRandom.

SeedRandom[] | reseed the pseudorandom generator, with the time of day |

SeedRandom[s] | reseed with the integer s |

Pseudorandom number generator seed.

In[9]:= |

Every single time RandomInteger, RandomReal, or 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 BlockRandom.

BlockRandom[expr] | evaluates expr with the current state of the pseudorandom generators localized |

Localizing the effects of using RandomInteger, RandomReal, or RandomComplex.

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 RandomInteger or RandomReal.

RandomInteger[dist],RandomReal[dist] | |

a pseudorandom number distributed by the random distribution dist | |

RandomInteger[dist,n],RandomReal[dist,n] | |

a list of n pseudorandom numbers distributed by the random distribution dist | |

RandomInteger[dist,{n_{1},n_{2},…}],RandomReal[dist,{n_{1},n_{2},…}] | |

an ××… array of pseudorandom numbers distributed by the random distribution dist |

Generating pseudorandom numbers with non-uniform distributions.

An additional use of pseudorandom numbers is for selecting from a list. RandomChoice selects with replacement and RandomSample samples without replacement.

RandomChoice[list, n] | choose n items at random from list |

RandomChoice[list,{n_{1},n_{2},…}] | an ××… array of values chosen randomly from list |

RandomSample[list, n] | a sample of size n from list |

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.