Main Content

兰德n

正态分布的随机数

Description

x = randnreturns a random scalar drawn from the standard normal distribution.

例子

x = randn((nreturns ann-by-nmatrix of normally distributed random numbers.

例子

x = randn((sz1,,,,。。。,,,,szNreturns ansz1-by-...-by-szN一系列随机数量sz1,,,,。。。,,,,szN指示每个维度的大小。例如,兰德(3,4)returns a 3-by-4 matrix.

例子

x = randn((szreturns an array of random numbers where size vectorszdefines尺寸(x)。例如,兰德([3 4])returns a 3-by-4 matrix.

例子

x = randn((___,,,,typename返回一系列随机数数据类型typename。这typename一世nput can be either"single"or"double"。You can use any of the input arguments in the previous syntaxes.

例子

x = randn((___,“喜欢”,preturns an array of random numbers likep; that is, of the same data type and complexity (real or complex) asp。您可以指定e一世thertypenameor“喜欢”,但不是两者。

x = randn((s,,,,___generates numbers from random number streams一世nstead of the default global stream. To create a stream, useRandStream。您可以指定sFollowed by any of the input argument combinations in previous syntaxes.

Examples

全部收缩

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

r =兰德(5)
r =5×50。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

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×21。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

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×50。5377 1.8339 -2.2588 0.8622 0.3188

Restore the state of the random number generator tos,然后创建一个随机数的新的1 x-5向量。值与以前相同。

rng(s); r1 = randn(1,5)
r1 =1×50。5377 1.8339 -2.2588 0.8622 0.3188

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.3077 x(:,:,:,:,:,2)= -0.4336 2.7694 0.3426 -1.3499 -1.3499 3.5784 3.5784 3.0349 x(3.0349 x(:0.,3)x(:,:-0.1241 0.7147 1.4897

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

r =兰德(1,,,,4,,,,"single"
r =1X4s一世ngle row vector0。5377 1.8339 -2.2588 0.8622
class(r)
ans ='单'

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×20。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));

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

p=s一世ngle([3 2; -2 1]);

Create an array of random numbers that is the same size and data type asp

x = randn(size(p),“喜欢”,,,,p)
X=2x2单基质0。5377 -2.2588 1.8339 0.8622
class(X)
ans ='单'

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

a =兰德(10,,,,1,,,,“喜欢”,1i)
a =10×1complex0.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

By default,兰德(n,“喜欢”,1i)generates random numbers from the standard complex normal distribution. The real and imaginary parts are independent normally distributed random variables with mean0and variance1/2。这covariance matrix is of the form[1/2 0;0 1/2]

z=兰德(50000,1,“喜欢”,1i);cov_z = cov(real(z),imag(z),1)
cov_z =2×20.4980 0.0007 0.0007 0.4957

要指定更一般的复杂正态分布,请定义平均值和协方差矩阵。例如,将平均值指定为 μ = 1 + 2 一世 and the covariance matrix as σ = [[ σ XX σ Xy σ yx σ yy 这是给予的 = [[ 2 - 2 - 2 4 这是给予的

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

转换先前生成的数据以遵循新定义的复杂正态分布。包括sqrt(2)当数据缩放时,因为原始分布中的真实和虚部的差异为1/2。

r = chol(sigma);z_comp = [real(z) imag(z)]; z = repmat(mu,50000,1) + z_comp*sqrt(2)*R*[1; 1i]; z(1:10)
ans =10×1complex1。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

输入参数

全部收缩

方形矩阵的大小,指定为整数值。

  • 如果n0, 然后X是一个空矩阵。

  • 如果n是negative, then it is treated as0

Data Types:s一世ngle|double|一世nt8|一世nt16|一世nt32|一世nt64|uint8|uint16|uint32|uint64

Size of each dimension, specified as separate arguments of integer values.

  • 如果任何维度的大小为0, 然后X是an empty array.

  • 如果任何维度的大小为negative, then it is treated as0

  • Beyond the second dimension,兰德n忽略尾随的尺寸为1。兰德(3,1,1,1)produces a 3-by-1 vector of random numbers.

Data Types:s一世ngle|double|一世nt8|一世nt16|一世nt32|一世nt64|uint8|uint16|uint32|uint64

每个维度的大小,指定为一个行向量of integer values. Each element of this vector indicates the size of the corresponding dimension:

  • 如果任何维度的大小为0, 然后X是an empty array.

  • 如果任何维度的大小为negative, then it is treated as0

  • Beyond the second dimension,兰德n忽略尾随的尺寸为1。兰德([3 1 1 1])produces a 3-by-1 vector of random numbers.

Example:sz=[[23 4]创建一个2 x-3 x-4阵列。

Data Types:s一世ngle|double|一世nt8|一世nt16|一世nt32|一世nt64|uint8|uint16|uint32|uint64

数据类型(类)要创建,指定为"double",,,,"single",或其他提供的类的名称兰德nsupport.

Example:兰德(5,“单身”)

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

Example:兰德(5,"like",p)

Data Types:s一世ngle|double
复杂的数字支持:万博1manbetxYes

随机数流,,,,specified as aRandStream目的。

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

更多关于

全部收缩

标准的真实和标准复杂正常分布

When generating random real numbers, the兰德nFunction generates data that follows the standard normal distribution:

F (( X = 1 2 π e X 2 / 2

For a random real variableX和mean 0 and variance 1.

When generating random complex numbers, such as when using the command兰德(。。。,“喜欢”,1一世), 这兰德n函数生成遵循标准复杂正态分布的数据:

F (( z = 1 π e | z | 2

For a random complex variablezwhose real and imaginary parts are independent normally distributed random variables with mean 0 and variance 1/2.

Tips

  • 兰德n是determined by the internal settings of the uniform pseudorandom number generator that underlies兰德,,,,兰德一世,,,,and兰德n。You can control that shared random number generator usingrng

Extended Capabilities

版本历史记录

Introduced before R2006a

eXpand all

Errors starting in R2013b

Not recommended starting in R2008b