The exponential distribution is a continuous probability distribution that defines the time between events in a process that occurs continuously and independently at a constant average rate.
Generate Exponentially Distributed Random Numbers in R
To generate exponentially distributed random numbers in R, use the rexp() function. The rexp() is a built-in R function that returns the values from the exponential distribution.
The rexp() function accepts the following two parameters.
- observations: It takes observations you want to see.
- rate: It is the estimated rate of events for the distribution; this is usually 1/expected service life or wait time.
Generate ten random numbers using the rexp() function.
set.seed(99) expo_dist_random_nums <- rexp(10, rate = 0.6) print(expo_dist_random_nums)
 0.28237284 4.25622953 0.11664528 0.34030952 1.87766520 2.98135398  0.16272462 0.01817234 3.22831023 0.45634704
In this example, we passed ten as observations and rate = 0.6; we got the output of 10 exponentially distributed random numbers.
Generating a histogram based on an exponential distribution
To create a histogram in R, use the hist() function and pass the exponential random numbers, probability=TRUE, col= gray(.9), main=”exponential mean=1400″ arguments.
set.seed(99) expo_dist_random_nums <- rexp(400, rate = 1/1400) hist(expo_dist_random_nums, probability=TRUE, col= gray(.9), main="exponential mean=1400") curve(dexp(x, 1/1400), add= T)
To generate a uniform distribution random numbers in R, use the runif() function.
To generate normal distributed random numbers in R, use the rnorm() function.
To generate binomial random numbers in R, use the rbinom() function.
To generate exponentially distributed random numbers in R, use the rexp() function.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.