兰德n
正态分布的随机数
句法
Description
x = randn
returns a random scalar drawn from the standard normal distribution.
x = randn((
returns ansz1,,,,。。。,,,,szN
)sz1
-by-...-by-szN
一系列随机数量sz1,,,,。。。,,,,szN
指示每个维度的大小。例如,兰德(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((
generates numbers from random number streams
,,,,___)s
一世nstead of the default global stream. To create a stream, useRandStream
。您可以指定s
Followed by any of the input argument combinations in previous syntaxes.
Examples
Matrix of Random Numbers
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
Reset Random Number Generator
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
3-D Array of Random Numbers
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 ='单'
Size Defined by Existing Array
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));
Size and Data Type Defined by Existing Array
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 mean0
and 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
要指定更一般的复杂正态分布,请定义平均值和协方差矩阵。例如,将平均值指定为 and the covariance matrix as 。
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
输入参数
n
-Size of square matrix
一世nteger value
方形矩阵的大小,指定为整数值。
如果
n
是0
, 然后X
是一个空矩阵。如果
n
是negative, then it is treated as0
。
Data Types:s一世ngle
|double
|一世nt8
|一世nt16
|一世nt32
|一世nt64
|uint8
|uint16
|uint32
|uint64
sz1,,,,。。。,,,,szN
-每个维度的大小(作为单独的参数)
一世nteger values
Size of each dimension, specified as separate arguments of integer values.
如果任何维度的大小为
0
, 然后X
是an empty array.如果任何维度的大小为negative, then it is treated as
0
。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
sz
-Size of each dimension (as a row vector)
一世nteger values
每个维度的大小,指定为一个行向量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 as
0
。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
typename
-数据类型(类)创建
"double"
((default) |"single"
数据类型(类)要创建,指定为"double"
,,,,"single"
,或其他提供的类的名称兰德n
support.
Example:兰德(5,“单身”)
p
-Prototype of array to create
numeric array
Prototype of array to create, specified as a numeric array.
Example:兰德(5,"like",p)
Data Types:s一世ngle
|double
复杂的数字支持:万博1manbetxYes
s
-随机数流
RandStream
目的
随机数流,,,,specified as aRandStream
目的。
Example:s=RandStream("dsfmt19937"); randn(s,[3 1])
更多关于
标准的真实和标准复杂正常分布
When generating random real numbers, the兰德n
Function generates data that follows the standard normal distribution:
For a random real variableX和mean 0 and variance 1.
When generating random complex numbers, such as when using the command兰德(。。。,“喜欢”,1一世)
, 这兰德n
函数生成遵循标准复杂正态分布的数据:
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
数据类型(类)必须是内置的MATLAB®numeric type. For other classes, the static
兰德n
method is not invoked. For example,兰德(sz,,,,'myclass')
does not invokemyclass.randn(sz)
。Size arguments must have a fixed size.
看代码生成工具箱功能的可变大小限制((MATLAB Coder)。
如果启用了外部呼叫,并且
兰德n
不是从内部调用parfor
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.
Thread-Based Environment
使用MATLAB®在后台运行代码backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
。
此功能完全支持基于线程的环境。万博1manbetx有关更多信息,请参阅在基于线程的环境中运行MATLAB功能。
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
这stream syntax
兰德(
是not supported on a GPU.s
,,,,___)您可以指定
typename
作为'gpuArray'
。如果you specifytypename
作为'gpuArray'
,数组的默认基础类型为double
。创建具有基础类型的GPU数组
datatype
,将基础类型指定为其他参数typename
。例如,x = randn(3,datatype,'gpuarray')
creates a 3-by-3 GPU array of random numbers with underlying typedatatype
。您可以指定the underlying type
datatype
作为one of these options:'双倍的'
'single'
“逻辑”
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
'int64'
'uint64'
You can also specify the numeric variable
p
作为agpuArray
。如果you specify
p
作为agpuArray
, 这underlying type of the returned array is the same asp
。
有关更多信息,请参阅在GPU上运行MATLAB功能((Parallel Computing Toolbox)。
分布式阵列
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
这stream syntax
兰德(
是not supported fors
,,,,___)codistibed
or分散式
arrays.您可以指定
typename
作为'codistributed'
or'分散式'
。如果you specifytypename
作为'codistributed'
or'分散式'
,返回数组的默认基础类型为double
。To create a distributed or codistributed array with underlying type
datatype
,将基础类型指定为其他参数typename
。例如,x = randn(3,datatype,“分布式”)
创建一个带有基础类型的随机数的3 x-3分布式矩阵datatype
。您可以指定the underlying type
datatype
作为one of these options:'双倍的'
'single'
“逻辑”
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
'int64'
'uint64'
You can also specify
p
作为acodistibed
or分散式
大批。如果you specify
p
作为acodistibed
or分散式
数组,返回数组的基础类型与p
。For additional
codistibed
syntaxes, see兰德n((codistibed)
((Parallel Computing Toolbox)。
有关更多信息,请参阅Run MATLAB Functions with Distributed Arrays((Parallel Computing Toolbox)。
版本历史记录
Introduced before R2006aR2022a: Match complexity with“喜欢”
,,,,and use“喜欢”
和RandStream
目的
这“喜欢”
输入支持真实万博1manbetx和复杂的原型阵列。例如:
r = randn(2,2,“喜欢”,1i)
r =0。3802 + 1.2968i 0.2254 - 0.9247i -1.5972 + 0.6096i -0.3066 + 0.2423i
所有语法都支持此功能。万博1manbetx另外,您现在可以使用“喜欢”
和aRandStream
对象作为第一个输入兰德n
。
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兰德(__,'like',p)
。例如:
A = single(pi); r = randn(4,4,'like',A); class(r)
ans =s一世ngle
通过A时,此功能不可用RandStream
目的作为the first input to兰德n
。
R2013b: Non-integer size inputs are not supported
Errors starting in R2013b
Specifying a dimension that is not an integer causes an error. UseFloor
to convert non-integer size inputs to integers.
R2008b:'种子'
,,,,'state'
,,,,and'twister'
一世nputs are not recommended
Not recommended starting in R2008b
这re are no plans to remove these inputs, which control the random number generator that underlies兰德
,,,,兰德一世
and兰德n
。However, therng
由于这些原因,建议使用功能:
这
'种子'
and'state'
generators are flawed.条款
'种子'
and'state'
是发电机的误导名称。'种子'
refers to the MATLAB v4 generator, not the seed initialization value.'state'
refers to the v5 generators, not the internal state of the generator.这sethree inputs unnecessarily use different generators for
兰德
and兰德n
。
有关更新代码的信息,请参阅Replace Discouraged Syntaxes of rand and randn。
也可以看看
MATLAB Command
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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:
How to Get Best Site Performance
选择中国网站(中文或英语)以获得最佳场地性能。其他Mathworks乡村网站未针对您所在的访问进行优化。
美洲
- América Latina((Español)
- 加拿大((English)
- 美国((English)
Europe
- Netherlands((English)
- Norway((English)
- Österreich((德意志)
- 葡萄牙((English)
- Sweden((English)
- Switzerland
- United Kingdom((English)