MATLAB中如何产生指定范围的随机数

转自:https://blog.csdn.net/helongfu/article/details/38806105

1、eg:要求产生20个10到15之间的随机数。

>> y=10+(15-10)*rand(4,5)    

y =
   13.2688   14.5186   10.9890   12.3996   14.2972
   12.4709   14.4546   10.1527   14.5236   14.0274
   13.8953   11.6708   13.7204   13.0493   12.8836

   13.5752   13.4937   12.5001   13.0883   10.9146

其中:   a、函数rand(x)或者rand(x,y)产生0~1之间均匀分布的随机矩阵

    b、>> format rat %以有理数形式输出
          >> rand(3)
ans =

        319/455       1695/2428       568/4437  
      677/1016      1601/2402      1086/1087  
      1185/2198       971/5451       461/2694  

2、eg:产生16个均值为0.8,方差为0.4的正态分布随机数(即:4阶正态分布矩阵)。

>> format short   %恢复默认输出格式
>> y=0.8+sqrt(0.4)*randn(4)

y =
    1.2980    0.6030    0.6672    1.2422
    0.2460    0.4395   -0.2745    0.9705
    1.0024    0.1513    1.1843    1.1126
    0.4469    0.2253    0.7255   -0.1380

其中:a、函数randn(x)或者randn(x,y)产生均值为0,方差为1的标准正态分布随机矩阵。

            b、正态分布均值u,方差σ^2,yi=u+σ*xi。

猜你喜欢

转载自blog.csdn.net/Candy_GL/article/details/81506876