Normally distributed random numbers - MATLAB randn (2024)

Table of Contents
Syntax Description Examples Matrix of Random Numbers Bivariate Normal Random Numbers Reset Random Number Generator 3-D Array of Random Numbers Specify Data Type of Random Numbers Size Defined by Existing Array Size and Data Type Defined by Existing Array Random Complex Numbers Random Complex Numbers with Specified Mean and Covariance Input Arguments n — Size of square matrix integer value sz1,...,szN — Size of each dimension (as separate arguments) integer values sz — Size of each dimension (as a row vector) integer values typename — Data type (class) to create "double" (default) | "single" p — Prototype of array to create numeric array s — Random number stream RandStream object More About Standard Real and Standard Complex Normal Distributions Tips Extended Capabilities C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Version History R2022a: Match complexity with "like", and use "like" with RandStream object R2014a: Match data type of an existing variable with 'like' R2013b: Non-integer size inputs are not supported R2008b: 'seed', 'state', and 'twister' inputs are not recommended See Also Topics MATLAB Command Americas Europe Asia Pacific

Normally distributed random numbers

collapse all in page

Syntax

X = randn

X = randn(n)

X = randn(sz1,...,szN)

X = randn(sz)

X = randn(___,typename)

X = randn(___,"like",p)

X = randn(s,___)

Description

X = randn returns a random scalar drawn from the standard normal distribution.

example

X = randn(n) returnsan n-by-n matrix of normallydistributed random numbers.

example

X = randn(sz1,...,szN) returnsan sz1-by-...-by-szN array ofrandom numbers where sz1,...,szN indicate the sizeof each dimension. For example, randn(3,4) returnsa 3-by-4 matrix.

example

X = randn(sz) returnsan array of random numbers where size vector sz defines size(X).For example, randn([3 4]) returns a 3-by-4 matrix.

example

X = randn(___,typename) returns an array of random numbers of data type typename. The typename input can be either "single" or "double". You can use any of the input arguments in the previous syntaxes.

example

X = randn(___,"like",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 either typename or "like", but not both.

X = randn(s,___) 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

collapse all

Matrix of Random Numbers

Open Live Script

Generate a 5-by-5 matrix of normally distributed random numbers.

r = randn(5)
r = 5×5 0.5377 -1.3077 -1.3499 -0.2050 0.6715 1.8339 -0.4336 3.0349 -0.1241 -1.2075 -2.2588 0.3426 0.7254 1.4897 0.7172 0.8622 3.5784 -0.0631 1.4090 1.6302 0.3188 2.7694 0.7147 1.4172 0.4889

Bivariate Normal Random Numbers

Open Live Script

Generate values from a bivariate normal distribution with specified mean vector and covariance matrix.

mu = [1 2];sigma = [1 0.5; 0.5 2];R = chol(sigma);z = repmat(mu,10,1) + randn(10,2)*R
z = 10×2 1.5377 0.4831 2.8339 6.9318 -1.2588 1.8302 1.8622 2.3477 1.3188 3.1049 -0.3077 1.0750 0.5664 1.6190 1.3426 4.1420 4.5784 5.6532 3.7694 5.2595

Reset Random Number Generator

Open Live Script

Save the current state of the random number generator and create a 1-by-5 vector of random numbers.

s = rng;r = randn(1,5)
r = 1×5 0.5377 1.8339 -2.2588 0.8622 0.3188

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.

rng(s);r1 = randn(1,5)

3-D Array of Random Numbers

Open Live Script

Create a 3-by-2-by-3 array of random numbers.

X = randn([3,2,3])
X = X(:,:,1) = 0.5377 0.8622 1.8339 0.3188 -2.2588 -1.3077X(:,:,2) = -0.4336 2.7694 0.3426 -1.3499 3.5784 3.0349X(:,:,3) = 0.7254 -0.2050 -0.0631 -0.1241 0.7147 1.4897

Specify Data Type of Random Numbers

Open Live Script

Create a 1-by-4 vector of random numbers whose elements are single precision.

r = randn(1,4,"single")
r = 1x4 single row vector 0.5377 1.8339 -2.2588 0.8622
class(r)
ans = 'single'

Size Defined by Existing Array

Open Live Script

Create a matrix of normally distributed random numbers with the same size as an existing array.

A = [3 2; -2 1];sz = size(A);X = randn(sz)
X = 2×2 0.5377 -2.2588 1.8339 0.8622

It is a common pattern to combine the previous two lines of code into a single line.

X = randn(size(A));

Size and Data Type Defined by Existing Array

Open Live Script

Create a 2-by-2 matrix of single-precision random numbers.

p = single([3 2; -2 1]);

Create an array of random numbers that is the same size and data type as p.

X = randn(size(p),"like",p)
X = 2x2 single matrix 0.5377 -2.2588 1.8339 0.8622
class(X)
ans = 'single'

Random Complex Numbers

Since R2022a

Open Live Script

Generate 10 random complex numbers from the standard complex normal distribution.

a = randn(10,1,"like",1i)
a = 10×1 complex 0.3802 + 1.2968i -1.5972 + 0.6096i 0.2254 - 0.9247i -0.3066 + 0.2423i 2.5303 + 1.9583i -0.9545 + 2.1460i 0.5129 - 0.0446i 0.5054 - 0.1449i -0.0878 + 1.0534i 0.9963 + 1.0021i

Random Complex Numbers with Specified Mean and Covariance

Since R2022a

Open Live Script

By default, randn(__,"like",1i) generates random numbers from the standard complex normal distribution. The real and imaginary parts are independent normally distributed random variables with mean 0 and variance 1/2. The covariance matrix for a 2-D random variable z=[Re(z),Im(z)] is [1/2 0; 0 1/2]. To show this default behavior, generate 50,000 random numbers using randn and calculate their covariance.

n = 50000;z = randn(n,1,"like",1i);cov_z = cov(real(z),imag(z),1)
cov_z = 2×2 0.4980 0.0007 0.0007 0.4957

To generate random numbers from a more general complex normal distribution with specific mean and covariance, transform the data generated from the default distribution. For an N-dimensional random variable z=[z1,z2,,zN] that follows a normal distribution with zero mean and unit covariance matrix, you can transform z to y=μ+zR. The variable y follows the normal distribution with mean μ and covariance matrix σ=RTR that is symmetric positive definite. For instance, specify the mean as μ=1+2i and the covariance matrix as σ=[σxxσxyσyxσyy]=[2-2-24].

mu = 1 + 2i;sigma = [2 -2; -2 4];

Perform the Cholesky decomposition of the covariance matrix. The result is an upper triangular matrix R such that sigma = R'*R. Scale the original data by also applying a factor of sqrt(2) because the variance of the real and imaginary parts in the original distribution is 1/2. Then, shift the scaled data to the specified mean.

R = chol(sigma);z_scaled = sqrt(2)*[real(z) imag(z)]*R*[1; 1i];y = mu + z_scaled;

Display the first 10 generated complex numbers.

y(1:10)
ans = 10×1 complex 1.7604 + 3.8331i -2.1945 + 6.4138i 1.4508 - 0.3002i 0.3868 + 3.0977i 6.0606 + 0.8560i -0.9090 + 8.2011i 2.0259 + 0.8850i 2.0108 + 0.6993i 0.8244 + 4.2823i 2.9927 + 2.0115i

Input Arguments

collapse all

nSize of square matrix
integer value

Size of square matrix, specified as an integer value.

  • If n is 0, then X isan empty matrix.

  • If n is negative, then it is treatedas 0.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

sz1,...,szNSize of each dimension (as separate arguments)
integer values

Size of each dimension, specified as separate arguments of integervalues.

  • If the size of any dimension is 0,then X is an empty array.

  • If the size of any dimension is negative, then itis treated as 0.

  • Beyond the second dimension, randn ignorestrailing dimensions with a size of 1. For example, randn(3,1,1,1) producesa 3-by-1 vector of random numbers.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

szSize of each dimension (as a row vector)
integer values

Size of each dimension, specified as a row vector of integervalues. Each element of this vector indicates the size of the correspondingdimension:

  • If the size of any dimension is 0,then X is an empty array.

  • If the size of any dimension is negative, then itis treated as 0.

  • Beyond the second dimension, randn ignores trailing dimensions with a size of 1. For example, randn([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

typenameData type (class) to create
"double" (default) | "single"

Data type (class) to create, specified as "double", "single", or the name of another class that provides randn support.

Example: randn(5,"single")

pPrototype of array to create
numeric array

Prototype of array to create, specified as a numeric array.

Example: randn(5,"like",p)

Data Types: single | double
Complex Number Support: Yes

sRandom number stream
RandStream object

Random number stream, specified as a RandStream object.

Example: s = RandStream("dsfmt19937"); randn(s,[3 1])

More About

collapse all

Standard Real and Standard Complex Normal Distributions

When generating random real numbers, the randn function generates data that follows the standard normal distribution:

f(x)=12πex2/2.

Here, x is a random real variable with mean 0 and variance 1.

When generating random complex numbers, such as when using the command randn(...,"like",1i), the randn function generates data that follows the standard complex normal distribution:

f(z)=1πe|z|2.

Here, z is a random complex variable whose real and imaginary parts are independent normally distributed random variables with mean 0 and variance 1/2.

Tips

  • The sequence of numbers produced by randn isdetermined by the internal settings of the uniform pseudorandom numbergenerator that underlies rand, randi,and randn. You can control that shared random numbergenerator using rng.

Extended Capabilities

Version History

Introduced before R2006a

expand all

Specifying a dimension that is not an integer causes an error. Use floor to convert non-integer size inputs to integers.

See Also

randi | rand | rng | RandStream | sprand | sprandn | randperm

Topics

  • Create Arrays of Random Numbers
  • Generate Random Numbers That Are Repeatable
  • Generate Random Numbers That Are Different
  • Random Numbers Within a Specific Range
  • Random Numbers Within a Sphere
  • Random Numbers from Normal Distribution with Specific Mean and Variance
  • Creating and Controlling a Random Number Stream
  • Class Support for Array-Creation Functions
  • Replace Discouraged Syntaxes of rand and randn
  • Why Do Random Numbers Repeat After Startup?

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Normally distributed random numbers - MATLAB randn (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Normally distributed random numbers - MATLAB randn (2024)
Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6318

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.