R rexp() Function - Generate Exponentially Distributed Random Numbers
The R rexp() function is used to generate random numbers that follow an exponential distribution.
The exponential distribution is often used to describe waiting times, such as equipment lifespan, customer arrival time intervals, etc.
The syntax of the rexp() function is as follows:
rexp(n, rate = 1) dexp(x, rate = 1) # 密度函数 pexp(q, rate = 1) # 累积分布函数
Parameter description:
nThe number of random numbers to generate.
rateThe rate parameter; the mean equals 1/rate.
Example
# Simulate customer arrival time intervals (one customer every 5 minutes on average)
set.seed(123)
arrival_times <- rexp(10, rate = 1/5)
print("Arrival intervals for 10 customers (minutes):")
print(round(arrival_times, 2))
print(paste("Average interval:", round(mean(arrival_times), 2), "minutes"))
# Generate a large amount of data to verify
set.seed(123)
large_sample <- rexp(10000, rate = 1/5)
print(paste("Mean of 10000 samples:", round(mean(large_sample), 2)))
set.seed(123)
arrival_times <- rexp(10, rate = 1/5)
print("Arrival intervals for 10 customers (minutes):")
print(round(arrival_times, 2))
print(paste("Average interval:", round(mean(arrival_times), 2), "minutes"))
# Generate a large amount of data to verify
set.seed(123)
large_sample <- rexp(10000, rate = 1/5)
print(paste("Mean of 10000 samples:", round(mean(large_sample), 2)))
Executing the above code produces the following output:
[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"Other extensions
R Language Example