在 MATLAB 中生成隨機數


在 MATLAB 程式設計中,我們可以使用各種型別的內建函式來生成不同型別的隨機數。隨機數只不過是從一組數字中隨機選擇的數字。

MATLAB 提供以下四個主要函式,根據我們的需求生成不同型別的隨機數

  • rand()

  • randn()

  • randi()

  • randperm()

讓我們藉助 MATLAB 中的示例程式詳細討論這四個函式中的每一個。

“rand” 函式

“rand()” 函式是 MATLAB 中的內建函式,可用於生成在 0 和 1 之間均勻分佈的隨機數。在此函式中,我們必須將隨機矩陣的維度指定為引數。

語法

rand

我們可以使用 rand() 函式生成一個均勻分佈的隨機數矩陣,在這種情況下,需要遵循以下語法

rand(row, col)

例如,要生成一個 3 x 5 的隨機數矩陣,

rand(3, 5)

現在,讓我們考慮一些 MATLAB 程式來了解 rand() 函式的操作。

示例

% MATLAB program to generate 6 uniformly distributed random numbers
for i = 1:6
   ran_num = rand;
   disp(ran_num)
end

輸出

0.033219
0.5965
0.1006
0.4967
0.6671
0.1800

示例

% MATLAB program to generate a 3 × 4 matrix of uniformly distributed random numbers
% Call the rand() function to generate a random matrix of size 3 × 4
Rand_Matrix = rand(3, 4);
% Display the randomly generated matrix
disp('The Random Matrix is:');
disp(Rand_Matrix);

輸出

The Random Matrix is:
   0.9883   0.4337   0.8890   0.3114
   0.5068   0.7758   0.6320   0.1136
   0.5637   0.3904   0.3751   0.8236

示例

% MATLAB program to generate a random vector of length 6
% Call the rand() function to generate a random vector of length 6
Rand_Vector = rand(1, 6);
% Display the randomly generated vector
disp('The Random Vector is:');
disp(Rand_Vector);

輸出

The Random Vector is:
   0.386889   0.313298   0.593926   0.268528   0.095952   0.965557

在上面的 MATLAB 程式中,我們使用了“rand()”函式生成了六個隨機數、一個大小為 3 x 4 的矩陣和一個長度為 6 的隨機向量。

"randn" 函式

在 MATLAB 中,randn() 函式用於生成正態分佈的隨機數。此函式基本上用於使用均值為 0、標準差為 1 的標準正態分佈生成隨機數、隨機矩陣或隨機向量。

語法

要生成一個正態分佈的隨機數列表

randn

要生成一個大小為 n x m 的隨機矩陣

randn(n, m)

要生成一個長度為 n 的隨機向量

randn(1, n)

現在,讓我們藉助示例程式瞭解 randn() 函式的實現。

示例

% MATLAB program to demonstrate the implementation of randn() function
% Generate 6 normally distributed random numbers
 disp('Random Numbers:')
for i = 1:6
   Rand_Num = randn;
   disp(Rand_Num);
end

% Generate a random matrix of size 4x3 with standard normal distribution
Rand_Matrix = randn(4, 3);
% Display the generated random matrix
disp('Random Matrix:');
disp(Rand_Matrix);

% Generate a random vector of length 6 with standard normal distribution
Rand_Vector = randn(1, 6);
% Display the generated random vector
disp('Random Vector:');

輸出

Random Numbers:
0.039510
-0.1805
0.2246
-1.3301
0.5415
0.6542
Random Matrix:
  -0.6967  -0.5581   0.7305
  -0.2204  -0.2242   0.3702
  -1.6478  -0.9363   1.4140
   1.0097   0.5562  -0.2543
Random Vector:
  -0.4225   0.3729  -0.8782   0.4408   0.2229  -0.7132

解釋

在此 MATLAB 程式中,randn() 函式用於從標準正態分佈生成 6 個正態分佈的隨機數、一個大小為 4x3 的隨機矩陣 (Rand_Matrix) 和一個長度為 6 的隨機向量 (Rand_Vector)。

“randi” 函式

在 MATLAB 中,randi() 函式用於在某個範圍內生成偽隨機整數。randi() 函式基本上使用均勻分佈生成指定最小值和最大值之間的隨機整數。

語法

要生成 'min' 和 'max' 之間的隨機整數

randi([min, max], 1)

要生成一個大小為 n x m 的隨機矩陣,其中整數在 'min' 和 'max' 之間

randi([min, max], n, m)

要生成一個長度為 n 的隨機向量,其中整數在 'min' 和 'max' 之間

randi([min, max], 1, n)

讓我們藉助示例程式瞭解此實現。

示例

% MATLAB program to demonstrate the implementation of randi() function
% Generate 6 random integers between 5 and 10
disp('Random Integers:')
for i = 1:6
   Rand_Int = randi([5, 10], 1);
   disp(Rand_Int);
end

% Generate a random matrix of size 3x4 with integers between 4 and 10
Rand_Matrix = randi([4, 10], 3, 4);
% Display the generated random matrix
disp('Random Matrix:');
disp(Rand_Matrix);

% Generate a random vector of length 6 with integers between 3 and 9
Rand_Vector = randi([3, 9], 1, 6);
% Display the generated random vector
disp('Random Vector:');
disp(Rand_Vector);

輸出

Random Integers:
5
7
10
10
10
6
Random Matrix:
    4    4    8    6
    4   10    8    9
    5    5    9    4
Random Vector:
   9   4   7   9   8   4

解釋

在此 MATLAB 程式中,randi() 函式用於生成 5 到 10 之間的 6 個隨機整數、一個大小為 3x4 的隨機矩陣 (Rand_Matrix) 和一個長度為 6 的隨機向量 (Rand_Vector)。

“randperm” 函式

在 MATLAB 中,“randperm()”函式用於生成整數的隨機排列。此函式有助於對資料進行洗牌或隨機化。

語法

randperm(max_value)

讓我們考慮一個示例程式來了解 randperm() 函式的實現。

示例

% MATLAB program to demonstrate the implementation of randperm()function
% Generate a random permutation of the integers from 1 to 7
Rand_Perm = randperm(7);
% Display the generated random permutation
disp('Random Permutation:');
disp(Rand_Perm);

輸出

Random Permutation:
   1   2   6   7   4   3   5

解釋

在此 MATLAB 程式中,randperm() 函式用於生成 1 到 7 的整數的隨機排列。結果儲存在變數“Rand_Perm”中。

結論

在本教程中,我們解釋了有關在 MATLAB 中生成隨機數的所有內容。但是,需要注意的是,每次執行程式時,上述輸出的值都會不同,這是因為 rand() 函式每次都會生成新的隨機值。

更新於: 2023-07-18

835 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告