R rnorm() Function - Generating Normal Random Numbers
The R rnorm() function is used to generate random numbers following a normal (Gaussian) distribution.
The normal distribution is the most common distribution in nature, widely used in statistical analysis and simulation experiments.
The syntax of the rnorm() function is as follows:
rnorm(n, mean = 0, sd = 1)
Parameter Description:
-
n The number of random numbers to generate.
-
mean The mean of the normal distribution, 0 by default.
-
sd The standard deviation of the normal distribution, 1 by default.
Example
Set a random seed to ensure reproducible results
set.seed
(
123
)
Generate 10 standard normal random numbers
random_nums
<-
rnorm
(
10
)
(
"Standard normal random numbers:"
)
(
round
(
random_nums,
4
)
)
Generate random numbers with mean 70 and standard deviation 10 (simulating exam scores)
set.seed
(
123
)
scores
<-
rnorm
(
1000
,
mean
=
70
,
sd
=
10
)
(
paste
(
"Mean of 1000 generated scores:"
,
round
(
mean
(
scores
)
,
2
)
)
)
(
paste
(
"Standard deviation:"
,
round
(
sd
(
scores
)
,
2
)
)
)
View the distribution
(
summary
(
scores
)
)
The output of the code above is:
[1] "标准正态分布随机数:"
[1] -0.5605 -0.2302 1.5587 0.0705 0.1293
[6] 1.7151 0.4609 -1.2651 -0.6869 -0.4457
[1] "生成 1000 个成绩的均值: 69.97"
[1] "标准差: 10.03"
Min. 1st Qu. Median Mean 3rd Qu. Max.
32.69 63.36 69.82 69.97 76.55 106.42
R Language Examples