Main Content
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
Topics
Generate Random Numbers
- Create Arrays of Random Numbers
Userand
,randi
,randn
, andrandperm
to 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 ofrand
andrandn
.
Control Random Number Generation
- Controlling Random Number Generation
This example shows how to use therng
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 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 theRandStream
class to control random number generation from the global stream. - Multiple Streams
This example usesRandStream
to create multiple, independent random number streams. - Creating and Controlling a Random Number Stream
This example shows how to useRandStream
to create random number streams and substreams.