Main Content

Random Number Generation

R2026b
Random numbers, streams, generator algorithms, and seeds

Use the rand, randi, randn, and randperm functions to generate random numbers from a shared global stream. Control the behavior and reproducibility of this stream by using the rng function. For more advanced control of random number streams, such as creating multiple independent streams or choosing normal transformation algorithms, use the RandStream object.

MATLAB provides several pseudorandom number generators that produce deterministic sequences of numbers that appear random. Given the same generator algorithm and seed, these sequences are reproducible. Although the numbers in the sequences are not truly random, they pass statistical tests of randomness and satisfy the independent and identically distributed condition. For this reason, numbers generated by pseudorandom number generators are commonly referred to as random numbers.

Functions

expand all

randUniformly distributed random numbers
randiUniformly distributed random integers
randnNormally distributed random numbers
randpermRandom permutation of integers
rngControl random number generator
RandStreamRandom number stream

Topics

Generate Random Numbers

Control Random Number Generation

Control Multiple Streams or Substreams

Featured Examples

Teaching Resources