Skip to content

[Term Entry] NumPy Random Module: .binomial() #7263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions content/numpy/concepts/random-module/terms/binomial/binomial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
Title: '.binomial()'
Description: 'Draw random samples from a binomial distribution.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Arrays'
- 'Data'
- 'Functions'
- 'Numpy'
- 'Probability'
- 'Statistics'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

In NumPy's `random` module, the **`.binomial()`** method generates random samples from a binomial distribution, representing the number of successful outcomes in `n` independent trials, each with a fixed probability `p` of success.

## Syntax

```pseudo
numpy.random.binomial(n, p, size=None)
```

**Parameters:**

- `n` (int or array-like of ints): Number of trials (must be non-negative).
- `p` (float or array-like of floats): Probability of success on each trial (must be between 0 and 1 inclusive).
- `size` (Optional): The shape of the output array. If `None`, a single value is returned. If given as a tuple, it specifies the shape of the output array.

**Return value:**

The `.binomial()` function returns one or more random integers representing the number of successes across `n` trials with success probability `p`.

- If `size` is `None`, a single integer is returned.
- If `size` is specified, an array of integers is returned with the given shape.

## Example: Generating Random Binomial Samples in NumPy

The following example simulates 10 independent trials, repeated 5 times. Each value in the result represents the number of successes in one set of 10 trials:

```py
import numpy as np

# Generate 5 random samples from a binomial distribution
result = np.random.binomial(n=10, p=0.5, size=5)

print(result)
```

A possible output of this code could be:

```shell
[3 5 4 5 4]
```

## Codebyte Example

In this Codebyte example, we generate a single random value from a binomial distribution with 15 trials and a 60% chance of success:

```codebyte/python
import numpy as np

# Define number of trials and probability
n = 15
p = 0.6

# Generate one random sample
sample = np.random.binomial(n, p)

print(sample)
```

> **Note:** The output will vary with each execution since it is randomly sampled.