Use the rand
, randn
,
and randi
functions to create sequences of pseudorandom
numbers. 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.
rand |
Uniformly distributed random numbers |
randn |
Normally distributed random numbers |
randi |
Uniformly distributed pseudorandom integers |
randperm |
Random permutation |
rng |
Control random number generation |
RandStream |
Random number stream |
This topic introduces random numbers in MATLAB®.
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.
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
.
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.
This topic shows how to use the RandStream
constructor
to control random number generation.
This example uses RandStream
to
create multiple, independent random number streams.
Creating and Controlling a Random Number Stream
This topic uses RandStream
to create
random number streams and substreams.