Written and maintained by CASRAI Editorial Board
Last updated
Short answer: rand in MATLAB returns uniformly distributed random numbers in the open interval (0,1), so it never returns exactly 0 or 1. For a random integer from 1 to 10, use randi(10) instead. Call rng at the top of a script to make a research analysis reproducible.
This page is for researchers and analysts who need random numbers in MATLAB for simulation, sampling or randomisation, and who must get the right range and be able to repeat the result later. It follows the current MathWorks documentation (read 2026-11-07).
What rand does in research
MathWorks describes rand as generating random floating-point numbers between 0 and 1 that are drawn from a uniform distribution. The values lie in the open interval (0,1) and are of class double by default. Typical research uses are Monte Carlo simulation (see what is Monte Carlo simulation), random assignment, bootstrap resampling and generating test data. For background on the environment, see the CASRAI guides to MATLAB documentation and the normal distribution in MATLAB.
Install or access (official source only)
MATLAB is a MathWorks product. rand, randi, randn, randperm and rng are built-in functions, and the examples here use nothing beyond them. Obtain MATLAB and its documentation from mathworks.com/help/matlab. Many universities hold campus licences, so ask your institution’s research computing or library service which applies to you.
Step by step: how to use rand()
- Open the MATLAB Command Window or a script.
- Set the seed first (see the seeds section below):
rng("default"). - Create the numbers:
n = 10000; r1 = rand(n,1);This makes an n-by-1 column vector from the uniform distribution on (0,1). MathWorks states that all values inr1are in the open interval (0,1). - For a matrix, give two dimensions:
rand(3,4)returns a 3-by-4 array.
Which function gives which range
| Function | What it returns | Range or property |
|---|---|---|
rand |
Uniformly distributed floating-point numbers | Open interval (0,1) |
randi |
Integers from a discrete uniform distribution | randi(10,n,1) gives integers in the closed interval [1,10] |
randn |
Standard normal numbers | Mean 0, standard deviation 1 |
randperm |
Random integers without repeated values | randperm(15,10) returns 10 values with no repeats, unlike randi |
Source: MathWorks, Random Numbers in MATLAB and Create Arrays of Random Numbers (read 2026-11-07).
How to generate a random number from 1 to 10 in MATLAB
Use randi(10) for one integer, or r2 = randi(10,n,1) for n of them. MathWorks documents the output as integer values in the closed interval [1,10]. Avoid round(10*rand): it can return 0 and gives the end values about half the probability of the others, so the integers are not equally likely.
Random numbers in another range
To scale rand to an interval (a,b), use r = a + (b-a).*rand(n,1). For integers in a custom range, randi([10 50],1,5) returns five integers between 10 and 50 inclusive.
Output and how to read it
- Every value from
randis strictly greater than 0 and strictly less than 1. If your protocol needs an inclusive 0 or 1,randalone does not provide it. - The default data type is double.
- The numbers are pseudorandom: the same generator state always gives the same sequence.
Seeds and reproducibility with rng
MathWorks says rng lets you control the seed and algorithm; the seed sets the initial state of the generator and lets you reproduce a sequence. The default generator is “twister”, the Mersenne Twister. The MathWorks page also lists other generators, including “pcg” and “xoshiro” (the page marks xoshiro as added in R2026b).
rng("default")at the top of a script resets the seed and generator to their defaults.rng(0,"twister")sets the seed to 0 and the generator to the Mersenne Twister.- To save and restore state:
s = rng;then laterrng(s). rng("shuffle")seeds from the clock, so each run differs.
For a methods section, report the generator, the seed and the MATLAB release, so another group can check a stochastic result.
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| Same numbers every time you restart MATLAB | The generator starts from the same default state in a new session | Call rng("shuffle") for a different sequence each run, and record the seed if you need to repeat it. |
| You expected 0 or 1 and never see them | rand uses the open interval (0,1) |
Use randi for integer endpoints. |
| Results differ between runs of your script | No rng call at the start |
Add rng(0,"twister") or rng("default") before any random call. |
| Repeated values where you wanted a shuffle | randi samples with repetition |
Use randperm. |
The MathWorks reference advises controlling the generator with rng rather than the older ‘seed’, ‘state’ or ‘twister’ inputs to rand.
How to print a variable in MATLAB
To show the generated values, use disp(r1): MathWorks documents disp as displaying the value of a variable without printing the variable name. To print formatted text around a value, use fprintf, for example fprintf('First value: %f; MathWorks notes that
', r1(1))fprintf text should end with the newline character . Omitting the semicolon after a statement also displays the result with its name.
Source: MathWorks, disp.
Free alternatives
| Tool | Relevant note |
|---|---|
| Scilab | Its documentation has a page titled “rand (Matlab function)” on uniformly distributed random numbers (help.scilab.org). |
| NumPy (Python) | See the CASRAI guide to numpy.random.randint for the integer equivalent. |
Is MATLAB being phased out?
The MathWorks documentation used for this page does not announce any retirement of MATLAB or of rand. MathWorks publishes a platform road map covering current MATLAB releases such as R2026a and R2026b, and the rng page describes generators added as recently as R2026b. For a project, check your own release and licence terms with your institution.
FAQ
How do I use rand()?
Call rand for one number in (0,1), rand(n,1) for a column of n, or rand(m,n) for an m-by-n array. Add rng("default") first if you need to repeat the result.
How do I generate a random number from 1 to 10 in MATLAB?
Use randi(10); the documented output is integers in [1,10].
Does rand include 0 and 1?
No. MathWorks documents the range as the open interval (0,1).
How do I print a variable in MATLAB?
Use disp(x) to show the value without its name, or fprintf for formatted text ending in .
Is MATLAB being phased out?
MathWorks documents R2026b as a current release and publishes a platform road map; the sources used here announce no retirement of MATLAB or rand.
Sources
- MathWorks, Random Numbers in MATLAB, read 2026-11-07.
- MathWorks, Create Arrays of Random Numbers, read 2026-11-07.
- MathWorks, disp, read 2026-11-07.
- MathWorks, Platform Road Map for MATLAB and Simulink.
- MathWorks, MATLAB documentation.







