Matlab Generate Vector of Random Numbers

How-to basics · Updated November 25, 2026

To generate a vector of random numbers in MATLAB, use the rand, randi, or randn functions with a size argument, for example rand(1,10) creates a 1-by-10 vector of uniform random numbers between 0 and 1. For integers, use randi([min,max],1,n), and for normally distributed numbers use randn(1,n).

Background: Why Random Numbers in MATLAB?

Random numbers are fundamental in numerical computing. In MATLAB, they are used for Monte Carlo simulations, statistical sampling, bootstrapping, randomizing data order, initializing weights in machine learning, and generating test inputs. MATLAB provides several built-in functions, each designed for a specific distribution or use case. Understanding the differences between them is key to writing correct and efficient code.

Core Functions: rand, randi, and randn

MATLAB’s random number generators are well-documented and reliable. Here are the three most common:

  • rand – Uniformly distributed numbers in the interval (0,1). Example: rand(1,5) returns a 1-by-5 vector.
  • randi – Uniformly distributed integers between specified bounds. Example: randi([1,100],1,10) gives 10 random integers from 1 to 100.
  • randn – Normally distributed numbers with mean 0 and standard deviation 1. Example: randn(1,4) returns a 1-by-4 vector of standard normal values.

All three accept two arguments: the number of rows and columns. For a row vector, use 1 as the first argument; for a column vector, use 1 as the second. For example, rand(5,1) creates a 5-by-1 column vector.

How to Specify the Size and Shape

The size argument can be a scalar, two scalars, or a vector of dimensions. For instance:

  • rand(100) – 100-by-100 matrix (scalar input creates a square matrix).
  • rand(2,3) – 2-by-3 matrix.
  • rand([1,10]) – row vector of length 10 (using a vector as size).

If you need a vector of a specific length, the cleanest way is rand(1,n) for a row vector or rand(n,1) for a column vector. You can also use rand(n,1)' to transpose a column into a row, but that is less efficient. For generating a vector directly, always specify the dimension explicitly to avoid accidental square matrices.

Choosing the Right Distribution

Different problems call for different distributions:

  • Uniform (rand) – Use when every value in a range is equally likely, such as simulating a fair die roll or random noise with fixed bounds.
  • Integer (randi) – Use for discrete outcomes like picking a random index or simulating a coin flip (with 0 and 1).
  • Normal (randn) – Use for data that clusters around a mean, such as measurement errors or natural phenomena.

To shift a uniform vector to a different interval [a,b], use a + (b-a)*rand(1,n). To shift a normal vector to have mean μ and standard deviation σ, use μ + σ*randn(1,n). For other distributions (exponential, Poisson, etc.), MATLAB has dedicated functions like exprnd, poissrnd, and betarnd in the Statistics and Machine Learning Toolbox.

Controlling the Random Seed for Reproducibility

By default, MATLAB’s random number generator starts from a different state each session, so results vary. To make results reproducible, set the seed with rng(seed) before generating numbers. For example:

rng(42);
x = rand(1,5);
% x will be the same every time you run this code with rng(42)

You can also use rng('default') to reset to the default seed, which is useful for debugging. The rng function controls the global generator; you can also create independent streams with RandStream for parallel computing or when you need separate, reproducible sequences.

Common Problems and Misconceptions

One common mistake is using rand(1,n) and expecting integers. rand always returns decimals; use randi for integers. Another issue is generating a vector but accidentally creating a matrix: rand(n) with a single scalar n creates an n-by-n matrix, not a vector. Always specify both dimensions for a vector.

Another misconception is that rand includes 0 or 1. The interval is (0,1), meaning 0 and 1 are not included, though the probability of hitting them exactly is negligible. For practical purposes, you can treat it as [0,1].

Performance-wise, generating a large vector is fast, but calling rand inside a loop repeatedly is inefficient. Instead, generate the entire vector at once and then index it. For example:

n = 10000;
data = rand(1,n); % generate all at once
for i = 1:n
% use data(i)
end

This is much faster than calling rand each iteration.

Alternatives and Advanced Options

If you need random numbers from a specific distribution, the Statistics and Machine Learning Toolbox offers random with a distribution name, such as random('Normal',0,1,1,100). For more control, you can use randsample for sampling without replacement, or randperm to generate a random permutation of integers. For reproducible parallel computations, use RandStream with a specific substream.

For generating random numbers in older MATLAB versions (before R2008b), the rand function used a different algorithm, but modern versions use the Mersenne Twister by default, which is well-tested and has a long period.

Frequently Asked Questions

How do I generate a vector of random integers between 1 and 10?

Use randi([1,10],1,n) where n is the length. For example, randi([1,10],1,5) returns a 1-by-5 vector of integers from 1 to 10 inclusive.

How do I make the random numbers the same every time I run my script?

Call rng(seed) with a fixed integer at the start of your script. For instance, rng(123) ensures the same sequence each run. This is essential for debugging and reproducible research.

What is the difference between rand and randn?

rand generates numbers uniformly distributed between 0 and 1, where every value has equal probability. randn generates numbers from a standard normal distribution (mean 0, standard deviation 1), so values cluster around 0 and can be negative or exceed 1.

Can I generate a random vector without the Statistics Toolbox?

Yes, the core functions rand, randi, and randn are part of base MATLAB, so no toolbox is required. For other distributions, you would need the toolbox or implement the inverse transform method yourself.

How do I generate a random vector of a specific length, say 1000?

Simply use rand(1,1000) for uniform, randi([1,100],1,1000) for integers, or randn(1,1000) for normal. The first argument (1) specifies rows, the second (1000) specifies columns, giving a row vector.

Is there a way to generate random numbers from a normal distribution with a specific mean and standard deviation?

Yes, transform the standard normal: mu + sigma*randn(1,n). For example, to get a vector of 100 numbers with mean 5 and standard deviation 2, use 5 + 2*randn(1,100).

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.