Open In App

numpy.random.laplace() in Python

Last Updated : 27 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Laplace distribution (also called double exponential distribution) models data with a sharp peak at the mean and heavier tails than a normal distribution. In Python, numpy.random.laplace() is used to generate random samples from Laplace distribution, defined by two parameters:

  • loc: mean (center of the distribution)
  • scale: diversity (spread of data around the mean)

Here, we generate a single random value from a Laplace distribution with mean = 0 and scale = 1.

Python
import numpy as np
x = np.random.laplace(loc=0, scale=1)
print(x)

Output
2.093642767866976

Explanation: Here, a single random value is generated from a Laplace distribution centered at 0 with spread 1. Each execution will produce a different random number.

Syntax

numpy.random.laplace(loc=0.0, scale=1.0, size=None)

Parameters:

  • loc (float): Mean (center) of the distribution.
  • scale (float): Standard deviation-like parameter (spread of the distribution).
  • size (int or tuple, optional): Shape of the output array. If None, returns a single value.

Return Value: out (ndarray or float) Random samples from a Laplace distribution.

Examples

Example 1: Generate 1000 random values with mean = 1.45 and scale = 15, then visualize them with a histogram.

Python
import numpy as np
import matplotlib.pyplot as plt

arr = np.random.laplace(1.45, 15, 1000)
plt.hist(arr, bins=30, density=True)
plt.show()

Output

randomLaplaceEx1

Explanation: histogram shows data centered around 1.45 with wide spread due to the high scale value of 15.

Example 2: Generate 1000 random values from a Laplace distribution, then use those values as input to generate another Laplace-distributed sample.

Python
import numpy as np
import matplotlib.pyplot as plt

arr1 = np.random.laplace(0.5, 12.45, 1000)
arr2 = np.random.laplace(arr1, 12.45, 1000)
plt.hist(arr2, bins=40, density=True)
plt.show()

Output

randomLaplaceEx2

Explanation: second sample (arr2) is generated by using the first Laplace sample (arr1) as the mean values, resulting in a more spread-out distribution.

Example 3: Generate a 2D NumPy array (3×3) of Laplace-distributed random values.

Python
import numpy as np
arr = np.random.laplace(0, 2, (3, 3))
print("2D Array:")
print(arr)

Output
2D Array:
[[-1.32414275  0.90883889 -0.41223986]
 [ 2.49728308  1.33401096 -9.83063736]
 [ 0.3321555   1.71840122  1.0742276 ]]

Explanation: Here, a 3×3 matrix is generated with values sampled from a Laplace distribution centered at 0 with scale 2.


Explore