Random Number Generation
R2026bUse 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
Topics
Generate Random Numbers
- Create Arrays of Random Numbers
Userand,randi,randn, andrandpermto create arrays of random numbers. - 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. - 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 ofrandandrandn.
Control Random Number Generation
- Control Random Number Generation
This example shows how to control random number generation using therngfunction. - Generate Random Numbers That Are Repeatable
This example shows how to repeat arrays of random numbers by specifying the generator algorithm and seed first. Every time you initialize the generator using the same algorithm and 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 theRandStreamclass to control random number generation from the global stream. - Multiple Streams
This example usesRandStreamto create multiple, independent random number streams. - Create and Control Random Number Streams
This example shows how to useRandStreamto create random number streams and substreams.
Related Information
Featured Examples
Teaching Resources
Descriptive Statistics and Probability Distributions
Learn the concepts of descriptive statistics, and discrete and continuous distributions.


