R rpois() Function - Generate Poisson Distribution Random Numbers

R 语言实例R Language Examples

The R rpois() function is used to generate random numbers that follow a Poisson distribution.

The Poisson distribution describes the number of times a random event occurs in a fixed time or space interval. It is often used to simulate website visits, phone call volume, etc.

The syntax of the rpois() function is as follows:

rpois(n, lambda)
dpois(x, lambda)   # 概率质量函数
ppois(q, lambda)   # 累积分布函数

Parameter description:

  • nThe number of random numbers to generate.

  • lambdaThe average number of events occurring per unit time (mean).

Example

# Simulate an average of 5 visits per hour, generate visit counts for 24 hours
set.seed(123)
hourly_visits <- rpois(24, lambda = 5)
print("Visits per hour for 24 hours:")
print(hourly_visits)

# dpois: probability of exactly 3 visits in one hour
prob_3 <- dpois(3, lambda = 5)
print(paste("Probability of exactly 3 visits:", round(prob_3, 4)))

# ppois: probability of more than 8 visits in one hour
prob_over_8 <- 1 - ppois(8, lambda = 5)
print(paste("Probability of more than 8 visits:", round(prob_over_8, 4)))

Executing the above code produces the following output:

[1] "24 小时每小时访问量:"
 [1] 4 6 7 3 4 6 6 7 3 1 5 6 6 6 4 6 3 3 4 5 4 10 4 4
[1] "恰好 3 次访问的概率: 0.1404"
[1] "超过 8 次访问的概率: 0.0681"

R 语言实例R Language Examples

Other Extensions