R rexp() Function - Generating Exponential Random Numbers
The R rexp() function is used to generate random numbers following an exponential distribution.
The exponential distribution is often used to describe waiting times, such as equipment lifetime and customer arrival intervals.
The syntax of the rexp() function is as follows:
rexp(n, rate = 1)
dexp(x, rate = 1) # 密度函数
pexp(q, rate = 1) # 累积分布函数
Parameter Description:
-
n The number of random numbers to generate.
-
rate The rate parameter; the mean equals 1/rate.
Example
Simulate customer arrival intervals (one customer every 5 minutes on average)
set.seed
(
123
)
arrival_times
<-
rexp
(
10
, rate
=
1
/
5
)
(
"Arrival intervals of 10 customers (minutes):"
)
(
round
(
arrival_times,
2
)
)
(
paste
(
"Average interval:"
,
round
(
mean
(
arrival_times
)
,
2
)
,
"minutes"
)
)
Generate a large amount of data for verification
set.seed
(
123
)
large_sample
<-
rexp
(
10000
, rate
=
1
/
5
)
(
paste
(
"Mean of 10000 samples:"
,
round
(
mean
(
large_sample
)
,
2
)
)
)
The output of the code above is:
[1] "10 位顾客的到达间隔(分钟):"
[1] 1.52 1.19 16.40 0.66 0.69 9.80 2.65 1.03 3.30 0.06
[1] "平均间隔: 3.73 分钟"
[1] "10000 个样本均值: 5.02"
R Language Examples