# Random Number Generation

Seeds, distributions, algorithms

Use the `rand`, `randn`, and `randi` functions to create sequences of pseudorandom numbers, and the `randperm` function to create a vector of randomly permuted integers. Use the `rng` function to control the repeatability of your results. Use the `RandStream` class when you need more advanced control over random number generation.

## Functions

expand all

 `rand` Uniformly distributed random numbers `randn` Normally distributed random numbers `randi` Uniformly distributed pseudorandom integers `randperm` Random permutation of integers
 `rng` Control random number generator `RandStream` Random number stream

## Topics

### Generate Random Numbers

Create Arrays of Random Numbers

Use `rand`, `randi`, `randn`, and `randperm` to create arrays of random numbers.

Random Numbers Within a Specific Range

This example shows how to create an array of random floating-point numbers that are drawn from a uniform distribution in a specific interval.

Random Integers

This example shows how to create an array of random integer values that are drawn from a discrete uniform distribution on a specific set of numbers.

Random Numbers from Normal Distribution with Specific Mean and Variance

This example shows how to create an array of random floating-point numbers that are drawn from a normal distribution having a specified mean and variance.

Random Numbers Within a Sphere

This example shows how to create random points within the volume of a sphere.

Why Do Random Numbers Repeat After Startup?

Avoid repetition of random number arrays when MATLAB® restarts.

Replace Discouraged Syntaxes of rand and randn

Replace Discouraged Syntaxes of `rand` and `randn`.

### Control Random Number Generation

Controlling Random Number Generation

This example shows how to use the `rng` function, which provides control over random number generation.

Generate Random Numbers That Are Repeatable

This example shows how to repeat arrays of random numbers by specifying the seed first. Every time you initialize the generator using the same seed, you always get the same result.

Generate Random Numbers That Are Different

This example shows how to avoid repeating the same random number arrays when MATLAB restarts.

### Control Multiple Streams or Substreams

Managing the Global Stream Using RandStream

This example shows how to use the `RandStream` class to control random number generation from the global stream.

Multiple Streams

This example uses `RandStream` to create multiple, independent random number streams.

Creating and Controlling a Random Number Stream

This example shows how to use `RandStream` to create random number streams and substreams.