rand - Uniformly distributed random numbers - MATLAB (original) (raw)
Uniformly distributed random numbers
Syntax
Description
X = rand
returns a random scalar drawn from the uniform distribution in the interval (0,1).
X = rand([n](#buiavoq-1-n))
returns ann
-by-n
matrix of uniformly distributed random numbers.
X = rand([sz1,...,szN](#buiavoq-1-sz1szN))
returns an sz1
-by-...-by-szN
array of random numbers where sz1,...,szN
indicate the size of each dimension. For example, rand(3,4)
returns a 3-by-4 matrix.
X = rand([sz](#buiavoq-1-sz))
returns an array of random numbers where size vector sz
defines size(X)
. For example,rand([3 4])
returns a 3-by-4 matrix.
X = rand(___,[typename](#buiavoq-1-typename))
returns an array of random numbers of data type typename
. Thetypename
input can be either "single"
or"double"
. You can use any of the input arguments in the previous syntaxes.
X = rand(___,"like",[p](#buiavoq-1-p))
returns an array of random numbers like p
; that is, of the same data type and complexity (real or complex) as p
. You can specify eithertypename
or "like"
, but not both.
X = rand([s](#mw%5F7b3a095f-0290-4394-8a40-c09de8009e3e),___)
generates numbers from random number stream s
instead of the default global stream. To create a stream, use RandStream. You can specify s
followed by any of the input argument combinations in previous syntaxes.
Examples
Matrix of Random Numbers
Generate a 5-by-5 matrix of uniformly distributed random numbers between 0 and 1.
r = 5×5
0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787
Random Numbers Within Specified Interval
Generate a 10-by-1 column vector of uniformly distributed numbers in the interval (-5,5). You can generate n
random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(n,1)
.
a = -5; b = 5; n = 10; r = a + (b-a).*rand(n,1)
r = 10×1
3.1472
4.0579
-3.7301 4.1338 1.3236 -4.0246 -2.2150 0.4688 4.5751 4.6489
Random Integers
Use the randi
function (instead of rand
) to generate 5 random integers from the uniform distribution between 10 and 50.
Reset Random Number Generator
Save the current state of the random number generator and create a 1-by-5 vector of random numbers.
r = 1×5
0.8147 0.9058 0.1270 0.9134 0.6324
Restore the state of the random number generator to s
, and then create a new 1-by-5 vector of random numbers. The values are the same as before.
r1 = 1×5
0.8147 0.9058 0.1270 0.9134 0.6324
3-D Array of Random Numbers
Create a 3-by-2-by-3 array of random numbers.
X = X(:,:,1) =
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975
X(:,:,2) =
0.2785 0.9649
0.5469 0.1576
0.9575 0.9706
X(:,:,3) =
0.9572 0.1419
0.4854 0.4218
0.8003 0.9157
Specify Data Type of Random Numbers
Create a 1-by-4 vector of random numbers whose elements are single precision.
r = 1x4 single row vector
0.8147 0.9058 0.1270 0.9134
Size Defined by Existing Array
Create a matrix of uniformly distributed random numbers with the same size as an existing array.
A = [3 2; -2 1]; sz = size(A); X = rand(sz)
X = 2×2
0.8147 0.1270
0.9058 0.9134
It is a common pattern to combine the previous two lines of code into a single line:
Size and Data Type Defined by Existing Array
Create a 2-by-2 matrix of single-precision random numbers.
Create an array of random numbers that is the same size and data type as p
.
X = rand(size(p),"like",p)
X = 2x2 single matrix
0.8147 0.1270
0.9058 0.9134
Random Complex Numbers
Generate 10 random complex numbers from the uniform distribution over a square domain with real and imaginary parts in the interval (0,1).
a = 10×1 complex
0.8147 + 0.9058i 0.1270 + 0.9134i 0.6324 + 0.0975i 0.2785 + 0.5469i 0.9575 + 0.9649i 0.1576 + 0.9706i 0.9572 + 0.4854i 0.8003 + 0.1419i 0.4218 + 0.9157i 0.7922 + 0.9595i
Input Arguments
n
— Size of square matrix
integer value
Size of square matrix, specified as an integer value.
- If
n
is0
, thenX
is an empty matrix. - If
n
is negative, then it is treated as0
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz1,...,szN
— Size of each dimension (as separate arguments)
integer values
Size of each dimension, specified as separate arguments of integer values.
- If the size of any dimension is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
rand
ignores trailing dimensions with a size of 1. For example,rand(3,1,1,1)
produces a 3-by-1 vector of random numbers.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz
— Size of each dimension (as a row vector)
integer values
Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:
- If the size of any dimension is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
rand
ignores trailing dimensions with a size of 1. For example,rand([3 1 1 1])
produces a 3-by-1 vector of random numbers.
Example: sz = [2 3 4]
creates a 2-by-3-by-4 array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
typename
— Data type (class) to create
"double"
(default) | "single"
Data type (class) to create, specified as "double"
,"single"
, or the name of another class that providesrand
support.
Example: rand(5,"single")
p
— Prototype of array to create
numeric array
Prototype of array to create, specified as a numeric array.
Example: rand(5,"like",p)
Data Types: single
| double
Complex Number Support: Yes
s
— Random number stream
RandStream
object
Random number stream, specified as a RandStream object.
Example: s = RandStream("dsfmt19937"); rand(s,[3 1])
More About
Pseudorandom Number Generator
The underlying number generator for rand
is a pseudorandom number generator, which creates a deterministic sequence of numbers that appear random. These numbers are predictable if the seed and the deterministic algorithm of the generator are known. While not truly random, the generated numbers pass various statistical tests of randomness, satisfying the independent and identically distributed (i.i.d.) condition, and justifying the name pseudorandom.
Tips
- The sequence of numbers produced by
rand
is determined by the internal settings of the uniform pseudorandom number generator that underliesrand
,randi
, andrandn
. You can control that shared random number generator usingrng.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- The data type (class) must be a built-in MATLAB® numeric type. For other classes, the static
rand
method is not invoked. For example,rand(sz,'myclass')
does not invokemyclass.rand(sz)
. - Size arguments must have a fixed size.
- See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
- If extrinsic calls are enabled and
rand
is not called from inside aparfor
loop, generated MEX files use the same random number state as MATLAB in serial code. Otherwise, the generated MEX code and standalone code maintain their own random number state that is initialized to the same state as MATLAB.
- If extrinsic calls are enabled and
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The rand
function supports GPU array input with these usage notes and limitations:
- You can specify
typename
as'gpuArray'
. If you specifytypename
as'gpuArray'
, the default underlying type of the array isdouble
.
To create a GPU array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = rand(3,datatype,'gpuArray')
creates a 3-by-3 GPU array of random numbers with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
- You can also specify the numeric variable
p
as agpuArray
.
If you specifyp
as agpuArray
, the underlying type of the returned array is the same asp
. - To use the stream syntax,
rand(`s`,___)
, on a GPU,s
must be a parallel.gpu.RandStream (Parallel Computing Toolbox) object.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
- The stream syntax
rand(`s`,___)
is not supported forcodistributed
ordistributed
arrays. - You can specify
typename
as'codistributed'
or'distributed'
. If you specifytypename
as'codistributed'
or'distributed'
, the default underlying type of the returned array isdouble
.
To create a distributed or codistributed array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = rand(3,datatype,'distributed')
creates a 3-by-3 distributed matrix of random numbers with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
- You can also specify
p
as acodistributed
ordistributed
array.
If you specifyp
as acodistributed
ordistributed
array, the underlying type of the returned array is the same asp
. - For additional
codistributed
syntaxes, see rand (codistributed) (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
R2022a: Match complexity with "like"
, and use "like"
with RandStream
object
The "like"
input supports both real and complex prototype arrays. For example:
r =
0.8147 + 0.9058i 0.6324 + 0.0975i 0.1270 + 0.9134i 0.2785 + 0.5469i
All syntaxes support this feature. Also, you can now use "like"
with a RandStream
object as the first input of rand
.
R2014a: Match data type of an existing variable with 'like'
To generate random numbers with the same data type as an existing variable, use the syntax rand(__,'like',p)
. For example:
A = single(pi); r = rand(4,4,'like',A); class(r)
This feature is not available when passing a RandStream
object as the first input to rand
.
R2013b: Non-integer size inputs are not supported
Specifying a dimension that is not an integer causes an error. Use floor to convert non-integer size inputs to integers.
R2008b: 'seed'
, 'state'
, and 'twister'
inputs are not recommended
There are no plans to remove these inputs, which control the random number generator that underlies rand
, randi
andrandn
. However, the rng function is recommended instead for these reasons:
- The
'seed'
and'state'
generators are flawed. - The terms
'seed'
and'state'
are misleading names for the generators.'seed'
refers to the MATLAB v4 generator, not the seed initialization value.'state'
refers to the v5 generators, not the internal state of the generator. - These three inputs unnecessarily use different generators for
rand
andrandn
.
For information on updating your code, see Replace Discouraged Syntaxes of rand and randn.