.exponential()
The .exponential()
function in NumPy random module generates random numbers from an exponential distribution, where the scale
parameter (1/λ) controls the spread, and the size
defines the shape of the output array.
Syntax
numpy.random.exponential(scale=1.0, size=None)
Parameters:
scale
(float, optional): The scale parameter (β = 1/λ) is also the distribution’s mean. Must be > 0. The default is1.0
.size
(int or tuple of ints, optional): Output shape. IfNone
(default), returns a single value. If an integer or tuple, returns an array of that shape.
Return value:
It returns random samples from the exponential distribution.
Its probability density function is:
$f(x; \frac{1}{\beta}) = \frac{1}{\beta} \exp(-\frac{x}{\beta}),$
Here, $\beta$ is the scale parameter (also the mean), and $\lambda = 1/\beta$ is the rate. This is a standard parameterization of the exponential distribution.
Example
The following code returns 10 random samples from an exponential distribution with a scale of 0.75
. Setting the seed ensures reproducible results:
import numpy as npnp.random.seed(23)results = np.random.exponential(scale=0.75, size=10)print(results)
The output of this code will be:
[0.54626666 2.20256852 1.08759608 0.24887788 0.18735185 0.86930237 0.13716657 0.3737313 0.72185379 0.39818209]
Codebyte Example
In this example, the waiting times between 100 calls to a help desk are simulated, where calls arrive randomly but on average every 5 minutes:
Here:
scale=5.0
means the average time between calls is 5 minutes.size=100
generates 100 waiting time values.np.random.exponential(...)
creates the random samples from an exponential distribution.- Only the first 10 values are printed for brevity.
This example demonstrates how exponential distribution can be used to model real-world scenarios like time intervals between incoming events.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours