A random number in MATLAB is any value drawn from a probability distribution using one of the language's built-in random number functions, most commonly rand, randi, randn, or randperm. The function rand returns a value sampled uniformly from the open interval (0, 1); randi returns a uniformly distributed integer in a range you specify; randn draws from a standard normal distribution with mean 0 and standard deviation 1; and randperm produces a random permutation of integers, which is useful for shuffling indices or dealing cards in simulation. All four call into the same underlying generator, which can be reseeded with rng(s) for reproducible results. Because MATLAB is the most common numerical computing environment in engineering and data science, learning the right function for each job is a small investment that pays back every time you need a test vector, a Monte Carlo sample, or a random stimulus.
If you only need a quick integer between two values and don't have MATLAB open, the Random Number Generator on this site draws from a secure source in your browser and lets you set a min, max, count, and duplicate policy without installing anything. For everything else — repeatable simulations, matrix fills, statistical sampling — the native MATLAB functions described below are the right answer.

Choosing the Right MATLAB Random Function
Before writing any code, decide what kind of output you need. A continuous value between 0 and 1 behaves very differently from an integer in 1..6, which behaves very differently from a sample drawn from a bell curve. Matching the function to the distribution avoids subtle bugs and saves you from scaling or rounding your own numbers afterwards.
| Function | Distribution | Typical output | Common use case |
|---|---|---|---|
| rand | Uniform on (0, 1) | Decimal between 0 and 1, exclusive | Probability thresholds, Monte Carlo inputs |
| randi | Uniform over integers | Integer in a closed interval [a, b] | Dice, lottery picks, random indices |
| randn | Standard normal (mean 0, std 1) | Real number, mostly between -3 and 3 | Noise, z-scores, statistical simulations |
| randperm | Random permutation | Vector containing each integer 1..n exactly once | Shuffling, sampling without replacement |
For generating a single random number in MATLAB between two integer bounds, randi is almost always the function you want. Reserve rand for cases where you truly need a fractional probability and are willing to scale it yourself.
Generating a Random Number in MATLAB Between Two Values
This is the most common task and the one most readers search for, so it deserves a dedicated walkthrough. MATLAB ships an inclusive integer sampler that takes the minimum and maximum as a two-element vector, which removes the off-by-one risk that catches people coming from other languages.
- Open MATLAB and confirm you are in the Command Window, not the Editor.
- Decide on your inclusive lower bound lo and inclusive upper bound hi. For example, lo = 1 and hi = 100.
- Optional but recommended: lock the generator to a known state so the run is reproducible. Type
rng(0);(or any integer seed) and press Enter. - Call
n = randi([lo hi]);to draw a single integer in the range [lo, hi]. - To draw several integers at once, replace the call with
v = randi([lo hi], 1, k);where k is the count. The vector v will hold k values, one per column. - Display the result with
disp(n);or just type the variable name and press Enter to echo it.
For a dice roll, the call collapses to d = randi([1 6]);. For a lottery-style draw between 1 and 49, use p = randi([1 49]);. Notice that the brackets around [lo hi] are part of the syntax — they create the two-element vector the function expects, not array indexing.
Generating Multiple Random Numbers Into a Matrix
MATLAB is built around matrices, so the random functions are designed to fill them in one shot. Knowing the size signatures saves you from looping, which is slow and contrary to MATLAB's vectorized style.
- Single value:
x = rand;returns one number drawn uniformly from (0, 1). - n-by-n matrix of uniform values:
A = rand(n);returns an n-by-n matrix of independent uniform samples. Userand(m,n)for an m-by-n shape. - n-by-n matrix of standard normal values:
B = randn(m,n);follows the same size rules asrand. - n-by-n matrix of integers in [a, b]:
C = randi([a b], m, n);again mirrors the size signature of the continuous functions. - Random permutation of 1..n:
p = randperm(n);returns a row vector containing each integer from 1 to n exactly once, in random order.
Because all of these calls are independent from each other, you can request thousands of values per second. If your code later needs the same sequence back, the seed you set with rng is the only thing tying a given output to a given input.
Seeding and Reproducibility
By default, MATLAB's random number functions draw from a stream seeded from the current time, which means two consecutive runs almost never produce the same sequence. That is fine for production code but fatal for debugging, because a transient "bug" you spot today may never recur tomorrow. The fix is a single line at the top of any script or function that depends on randomness.
- Pick any integer seed — common conventions are
0,42, or today's date as an integer such as20251112. - Place
rng(seed);as the first executable line of your script. - Run the script and record the output. The seed freezes the generator state, so the output is now a deterministic function of the seed.
- To return to non-reproducible behavior, call
rng('shuffle');, which reseeds from the clock at call time. - For team-shared results, switch to
rng(0, 'twister');or another named generator so that everyone gets the same stream regardless of MATLAB version.
If you maintain tests in MATLAB, treat rng the way you treat a database fixture: every test should set its own seed at the start so a failure can be replayed byte-for-byte later.
Common MATLAB Random Number Patterns
Beyond the raw function calls, a handful of idioms cover most real-world needs. Memorizing them is faster than reinventing them each time.
- Uniform real in [a, b]:
x = a + (b - a) * rand;scales a unit uniform draw into any continuous interval. - Pick a random element from an array:
val = arr(randi(numel(arr)));works on any array shape, including cell arrays of strings. - Shuffle a vector in place:
v = v(randperm(numel(v)));reorders v randomly using a permutation index. - Sample without replacement (k picks from 1..n):
picks = randperm(n, k);returns k unique integers. - Normal with non-unit parameters:
x = mu + sigma * randn;shifts the standard normal to have mean mu and standard deviation sigma.
For high-throughput Monte Carlo work, preallocate a vector with out = zeros(1, N); then assign out(i) = randi([lo hi]); inside a parfor loop. MATLAB's Parallel Computing Toolbox will then distribute the calls across workers without contention on the generator stream.
When a Browser Tool Is the Better Choice
MATLAB is overkill if all you need is one integer for a giveaway, a quick classroom draw, or a tiebreaker. The Random Number Generator on this site produces fair integers in any inclusive range, supports up to 1,000 draws per click, and runs entirely client-side so nothing leaves your machine. It is also handy when you need a number during a meeting and don't want to fire up a license server.
For related quick draws, a Dice Roller covers d4 through d20 with proper polyhedral distributions, a Coin Flip gives you a fair heads-or-tails call without an unfair physical coin, and a Random Team Generator splits a roster into balanced groups when you need more than one number at once. For more general writing-style tasks that pair with simulations — usernames for synthetic test data, say — the username generator guide on this site walks through picking handle-style identifiers that read well in logs.
Quick Reference: MATLAB Random Functions at a Glance
| Goal | One-line MATLAB call | Notes |
|---|---|---|
| One uniform float in (0, 1) | x = rand; | Open interval, so 0 and 1 are excluded. |
| One integer in [1, 6] | d = randi([1 6]); | Inclusive on both ends. |
| 10 integers in [1, 100] | v = randi([1 100], 1, 10); | Row vector, length 10. |
| 3-by-3 standard normal matrix | M = randn(3); | Mean 0, standard deviation 1. |
| Uniform real in [a, b] | x = a + (b - a) * rand; | Linear scaling of a unit draw. |
| Random shuffle of indices 1..n | p = randperm(n); | Each integer appears exactly once. |
| k unique picks from 1..n | picks = randperm(n, k); | Requires k <= n. |
| Reproducible run | rng(42); % before any draws | Seeds the default generator. |
The patterns above cover roughly ninety percent of day-to-day MATLAB random number work. Anything more exotic — different distributions, streaming generators, GPU-backed sampling — falls under the random function family documented in the official MathWorks random number generation documentation, which is the reference most working engineers keep bookmarked.
Related reading: How to Create Random Teams From Any Roster.
Related reading: How to Generate Random Characters in Python.