R – How to Generate a Random Integer

distributionsrrandom-generationsample

I am trying to generate a random integer that follows this distribution.

$$ P(X=k)=p(1-p)^{k-1}$$
for
$$ k=1,2,…$$

I tried to use sample with some numbers, but i do not know how to use the distribution.

Best Answer

This is the probability mass function of the geometric distribution.

If you use R: To sample from the geometric distribution, first specify the probability $p$ and then use the rgeom function:

# The geometric distribution has one parameter, a probability between 0 and 1.
p <- 0.3
# How many numbers do you want to generate?
n <- 10
# Draw random sample from the geometric distribution.
rgeom(n, p)
#>  [1] 7 0 6 8 0 1 0 0 2 2
Related Question