Skip to main content
TopMiniSite

Back to all posts

How to Generate A Random Number In MATLAB?

Published on
5 min read
How to Generate A Random Number In MATLAB? image

Best MATLAB Tools to Generate to Buy in December 2025

1 Learning to Program with MATLAB: Building GUI Tools

Learning to Program with MATLAB: Building GUI Tools

BUY & SAVE
$64.48 $100.95
Save 36%
Learning to Program with MATLAB: Building GUI Tools
2 MATLAB: A Practical Introduction to Programming and Problem Solving

MATLAB: A Practical Introduction to Programming and Problem Solving

BUY & SAVE
$48.15 $66.95
Save 28%
MATLAB: A Practical Introduction to Programming and Problem Solving
3 MATLAB Symbolic Algebra and Calculus Tools

MATLAB Symbolic Algebra and Calculus Tools

BUY & SAVE
$35.77 $59.99
Save 40%
MATLAB Symbolic Algebra and Calculus Tools
4 Antenna and EM Modeling with MATLAB Antenna Toolbox

Antenna and EM Modeling with MATLAB Antenna Toolbox

BUY & SAVE
$121.00 $140.95
Save 14%
Antenna and EM Modeling with MATLAB Antenna Toolbox
5 Spectral Methods in MATLAB (Software, Environments, Tools)

Spectral Methods in MATLAB (Software, Environments, Tools)

  • AFFORDABLE PRICES ON QUALITY USED BOOKS FOR SAVVY READERS.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING PRE-LOVED BOOKS.
  • ENJOY RAPID DELIVERY AND EASY RETURNS FOR A SEAMLESS SHOPPING EXPERIENCE.
BUY & SAVE
$97.41
Spectral Methods in MATLAB (Software, Environments, Tools)
6 Learning to Program with MATLAB: Building GUI Tools

Learning to Program with MATLAB: Building GUI Tools

BUY & SAVE
$44.97 $95.95
Save 53%
Learning to Program with MATLAB: Building GUI Tools
7 Design of Satellite Communication Toolbox for MATLAB®

Design of Satellite Communication Toolbox for MATLAB®

BUY & SAVE
$100.00
Design of Satellite Communication Toolbox for MATLAB®
8 Kaisi Professional Electronics Opening Pry Tool Repair Kit +S-130 Insulation Silicone Soldering Mat Repair Mat Nylon Spudgers and Anti-Static Tweezers for Cellphone iPhone Laptops Tablets and More

Kaisi Professional Electronics Opening Pry Tool Repair Kit +S-130 Insulation Silicone Soldering Mat Repair Mat Nylon Spudgers and Anti-Static Tweezers for Cellphone iPhone Laptops Tablets and More

  • VERSATILE KIT: PERFECT FOR REPAIRING SMARTPHONES, TABLETS, & LAPTOPS!
  • DURABLE TOOLS: STAINLESS STEEL & NYLON TOOLS FOR LONG-LASTING USE.
  • HEAT RESISTANT: WITHSTANDS HIGH TEMPS, IDEAL FOR SOLDERING TASKS.
BUY & SAVE
$15.99 $24.88
Save 36%
Kaisi Professional Electronics Opening Pry Tool Repair Kit +S-130 Insulation Silicone Soldering Mat Repair Mat Nylon Spudgers and Anti-Static Tweezers for Cellphone iPhone Laptops Tablets and More
9 An Introduction to Finite Element Analysis Using Matlab Tools (Synthesis Lectures on Mechanical Engineering)

An Introduction to Finite Element Analysis Using Matlab Tools (Synthesis Lectures on Mechanical Engineering)

BUY & SAVE
$64.99
An Introduction to Finite Element Analysis Using Matlab Tools (Synthesis Lectures on Mechanical Engineering)
10 MATLAB: A Practical Introduction to Programming and Problem Solving

MATLAB: A Practical Introduction to Programming and Problem Solving

BUY & SAVE
$40.79
MATLAB: A Practical Introduction to Programming and Problem Solving
+
ONE MORE?

To generate a random number in MATLAB, you can use the built-in function rand, randi, or randn. Here's how each of these functions work:

  1. rand: This function returns a single random number between 0 and 1. For example, to generate a random number, you can write:

randomNumber = rand;

  1. randi: This function is used to generate random integers within a specified range. To generate a single random integer between a minimum value minVal and a maximum value maxVal, you can use:

randomInt = randi([minVal, maxVal]);

  1. randn: This function is used to generate random numbers from a standard normal distribution with a mean of 0 and a standard deviation of 1. To generate a single random number from this distribution, you can use:

randomNumber = randn;

Note that randn can return negative values as it samples from a normal distribution.

You can also generate arrays of random numbers by specifying the desired size in the function call. For example:

randomArray = rand(3, 4); % Generates a 3x4 array of random numbers between 0 and 1 randomIntArray = randi([1, 10], 1, 5); % Generates a 1x5 array of random integers between 1 and 10 randomNormalArray = randn(2, 3); % Generates a 2x3 array of random numbers from a standard normal distribution

Remember to use appropriate seeds if you want to generate reproducible random numbers. This can be done by using the rng function before generating random numbers. For example:

rng(42); % Sets the random number generator seed to 42 randomNumber = rand;

How to generate a random sequence of numbers in MATLAB?

To generate a random sequence of numbers in MATLAB, you can use the rand, randi, or randn functions.

  1. rand generates a sequence of random numbers between 0 and 1.

sequence = rand(1, n);

Here, n is the length of the sequence you want to generate.

  1. randi generates a sequence of random integers within a specified range.

sequence = randi([minValue, maxValue], 1, n);

Here, minValue and maxValue define the range of possible values, and n specifies the length of the sequence.

  1. randn generates a sequence of random numbers from a standard normal distribution.

sequence = randn(1, n);

Again, n determines the length of the sequence.

You can replace 1 with the desired number of rows and n with the desired number of columns if you want a matrix instead of a sequence.

How to generate a random matrix in MATLAB?

You can generate a random matrix in MATLAB using the "rand" function. The "rand" function generates uniformly distributed random numbers between 0 and 1. To create a random matrix, you can specify the dimensions of the matrix as inputs to the "rand" function.

Here is an example of how to generate a random matrix of size 3x3:

matrix = rand(3, 3);

This will create a 3x3 matrix filled with random numbers between 0 and 1. You can adjust the dimensions of the matrix by changing the input values to the "rand" function.

If you want to generate random numbers from a specific distribution or with different characteristics, you can use other functions such as "randn" for normally distributed random numbers or "randi" for random integers.

What is the command to generate a random number from a discrete distribution in MATLAB?

The command to generate a random number from a discrete distribution in MATLAB is randsample.

Here's an example of how to use it:

% Define the values of the discrete distribution values = [1, 2, 3, 4, 5]; % Define the corresponding probabilities for each value probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]; % Generate a random number from the discrete distribution randomNumber = randsample(values, 1, true, probabilities);

In this example, values represent the possible values of the discrete distribution, probabilities represent the corresponding probabilities for each value, and randomNumber will store the randomly generated value. The fourth argument of randsample is set to true to indicate that the probabilities are specified.

How to generate multiple random numbers at once in MATLAB?

To generate multiple random numbers at once in MATLAB, you can use the rand function in combination with specifying the size of the random number matrix.

Here is an example of generating a 3x3 matrix of random numbers:

rand_numbers = rand(3, 3);

This will generate a 3x3 matrix rand_numbers where each element is a random number between 0 and 1.

If you want to generate random numbers within a specific range, you can scale and shift the random values. For example, to generate random numbers between 0 and 100:

rand_numbers = rand(3, 3) * 100;

Similarly, if you want to generate random integers within a specific range, you can use the randi function. For example, to generate random integers between 1 and 10:

rand_ints = randi([1, 10], 3, 3);

This will generate a 3x3 matrix rand_ints where each element is a random integer between 1 and 10.