Python:NumPy .geometric()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 25, 2025
Contribute to Docs

The .geometric() function in numpy.random returns random samples from a geometric distribution based on a given probability of success, with the option to control the number of samples through the size parameter.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

numpy.random.geometric(p, size=None)

Parameters:

  • p (float or array_like): Probability of success for each trial. Must be in the range (0, 1].
  • size (int or tuple of ints, optional): Output shape. If None, a single value is returned.

Return value:

Returns random samples drawn from the geometric distribution, representing the number of trials until the first success.

The probability mass function of geometric distribution is:

$$ f(k) = (1 - p)^{k - 1} \ p $$

p in this case is the success probability of an individual trial.

Example

The following code returns 10 random samples from a geometric distribution with the probability of success set to 0.35:

import numpy as np
# Setting the seed ensures reproducible results
np.random.seed(42)
results = np.random.geometric(p=0.35, size=10)
print(results)

The output of this code would be:

[2 7 4 3 1 1 1 5 3 3]

Codebyte Example

This Codebyte simulates 100 coin flips with an unfair coin (35% chance of Heads), then counts how many trials resulted in a success on the first attempt:

Code
Output

Here:

  • p=0.35: Probability of success (e.g., the coin landing on Heads).
  • size=100: Generates 100 random samples.
  • np.random.geometric(p, size): Returns the number of trials needed to get the first success.

This example demonstrates how geometric distributions can model binary outcome processes like coin tosses where the goal is to measure how many attempts are needed to succeed once.

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours