R rpois() Function - Generating Poisson Random Numbers
The R rpois() function is used to generate random numbers following a Poisson distribution.
The Poisson distribution describes the number of random events occurring in a fixed interval of time or space, often used to simulate website visits, incoming calls, and so on.
The syntax of the rpois() function is as follows:
rpois(n, lambda)
dpois(x, lambda) # 概率质量函数
ppois(q, lambda) # 累积分布函数
Parameter Description:
-
n The number of random numbers to generate.
-
lambda The average number of events per unit time (the mean).
Example
Simulate a website with 5 visits per hour on average; generate 24 hours of visit counts
set.seed
(
123
)
hourly_visits
<-
rpois
(
24
, lambda
=
5
)
(
"Hourly visits over 24 hours:"
)
(
hourly_visits
)
dpois: probability of exactly 3 visits in one hour
prob_3
<-
dpois
(
3
, lambda
=
5
)
(
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
)
(
paste
(
"Probability of more than 8 visits:"
,
round
(
prob_over_8,
4
)
)
)
The output of the code above is:
[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 Language Examples