1.rand()函数
Generate a 5-by-5 matrix of uniformly distributed random numbers between 0 and 1.
r = rand(5)
r =
0.5468 0.6791 0.8852 0.3354 0.6538
0.5211 0.3955 0.9133 0.6797 0.4942
0.2316 0.3674 0.7962 0.1366 0.7791
0.4889 0.9880 0.0987 0.7212 0.7150
0.6241 0.0377 0.2619 0.1068 0.9037
Random Numbers Within Specified Interval
Generate a 10-by-1 column vector of uniformly distributed numbers in the interval [-5,5]
r = -5 + (5+5)*rand(10,1)
r =
3.1472
4.0579
-3.7301
4.1338
1.3236
-4.0246
-2.2150
0.4688
4.5751
4.6489
In general, you can generate N random numbers in the interval [a,b] with the formula r = a + (b-a).*rand(N,1);.
2.randi()函数
Generate a 5-by-5 matrix of random integers between 1 and 10. The first input to randi indicates the largest integer in the sampling interval (the smallest integer in the interval is 1).
r = randi(10,5)
r =
4 3 2 10 5
6 5 4 10 6
5 1 2 1 10
7 10 5 8 5
7 2 4 3 10
Random Integers Within Specified Interval
Generate a 10-by-1 column vector of uniformly distributed random integers from the sample interval [-5,5].
r = randi([-5,5],10,1)
r =
3
4
-4
5
1
-4
-2
1
5
5
3.randn()函数
Generate a 5-by-5 matrix of normally distributed random numbers.
r = randn(5)
r =
1.0347 0.8884 1.4384 -0.1022 -0.0301
0.7269 -1.1471 0.3252 -0.2414 -0.1649
-0.3034 -1.0689 -0.7549 0.3192 0.6277
0.2939 -0.8095 1.3703 0.3129 1.0933
-0.7873 -2.9443 -1.7115 -0.8649 1.1093