Randomness is a common theme in many scientific and engineering applications. Often, we need to generate random data sets for testing, simulation, or analysis. MATLAB provides a built-in function, normrnd, that can be used for generating data sets with normal (Gaussian) distribution. In this article, we will explore how to use the normrnd function, its syntax, and some of its parameters.
Syntax of normrnd
The syntax of the normrnd function is straightforward. It takes two arguments: the mean and the standard deviation of the normal distribution. The output is a random number drawn from the specified normal distribution. Here is the basic syntax of the normrnd function:
y = normrnd(mu, sigma)
where y is the generated random number, mu is the mean, and sigma is the standard deviation.
Generating a single random number
Let's start by generating a single random number using the normrnd function. Suppose we want to simulate the result of rolling a fair six-sided die. We can model this by generating a random number between 1 and 6 with equal probability. Here is the code to do this:
x = normrnd(3.5, sqrt(35/12))
The mean of a six-sided die is (1+2+3+4+5+6)/6 = 3.5, and the variance is ((1-3.5)^2 + (2-3.5)^2 + ... + (6-3.5)^2)/6 = 35/12. The standard deviation is the square root of the variance. We can then round the result to the nearest integer to simulate the roll of a die:
result = round(x)
Note that the actual result of rolling a die is discrete, but the result of the normrnd function is continuous. We need to round the result to simulate the discrete nature of the die.
Generating multiple random numbers
Often, we need to generate a series of random numbers instead of just one. We can use the normrnd function in a loop to achieve this. For example, suppose we want to generate 10 random numbers with mean 0 and standard deviation 1. We can use the following code:
for i = 1:10
x(i) = normrnd(0,1);
end
This will generate an array x with 10 random numbers drawn from a normal distribution with mean 0 and standard deviation 1.
Visualizing random data sets
Visualizing random data sets is a useful way to gain insight into their properties. MATLAB provides several functions for plotting histograms and probability density functions (PDFs) of data sets. Let's generate a large data set and plot its histogram and PDF.
Suppose we want to generate a data set with mean 10 and standard deviation 3. We can use the following code to generate 10,000 random numbers:
data = normrnd(10,3,10000,1);
This will generate an array data with 10,000 random numbers drawn from a normal distribution with mean 10 and standard deviation 3. We can now plot its histogram and PDF using the hist and normpdf functions:
hist(data, 50); % plot histogram with 50 bins
hold on;
x = linspace(0, 20, 100); % generate 100 points between 0 and 20
y = normpdf(x, 10, 3); % compute PDF with mean 10 and std 3
plot(x, y, 'r', 'LineWidth', 2); % plot PDF in red with line width 2
hold off;
This code will plot a histogram of the data with 50 bins and a red curve representing the PDF of the normal distribution with mean 10 and standard deviation 3. The resulting plot should look like this:

Conclusion
In this article, we have explored how to use the normrnd function in MATLAB to generate random data sets with normal distribution. We have seen how to generate a single random number, how to generate multiple random numbers in a loop, and how to visualize random data sets using histograms and PDFs. The normrnd function is a powerful tool for generating random data for testing, simulation, or analysis in many scientific and engineering applications.