---
title: "How to use Random Numbers in NumPy?"  
description: "How to use Random Numbers in NumPy?"  
author: "Anubhav Sharma"  
published: 2025-11-25  
updated: 2025-11-25  
canonical: https://www.mindstick.com/interview/34417/how-to-use-random-numbers-in-numpy  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 4 minutes  

---

# How to use Random Numbers in NumPy?

## NumPy Random Numbers

[NumPy](https://www.mindstick.com/interview/34416/how-to-filter-an-array-using-numpy) provides random number functions through:

```plaintext
numpy.random
```

There are many ways to generate random numbers:

### 1. Random Integer (`randint`)

```python
import numpy as np

# Random integer between 0 and 10 (exclusive)
x = np.random.randint(0, 10)
print(x)
```

Generate **multiple** random integers:

```python
arr = np.random.randint(0, 100, size=5)
print(arr)
```

### 2. Random Floats (`rand`, `randn`, `random`)

#### Uniform distribution (0 → 1)

```python
x = np.random.rand()
print(x)
```

Multi-dimensional:

```python
arr = np.random.rand(3, 2)
print(arr)
```

#### Normal distribution (mean=0, std=1)

```python
x = np.random.randn(5)
print(x)
```

#### General random floats

```python
arr = np.random.random((2, 3))
print(arr)
```

### 3. Random Choice (`choice`)

Select random values from a list or array:

```python
items = np.array([10, 20, 30, 40, 50])

x = np.random.choice(items)
print(x)
```

Select multiple:

```python
arr = np.random.choice(items, size=3)
print(arr)
```

With replacement disabled:

```python
np.random.choice(items, size=3, replace=False)
```

### 4. Random Shuffle

Shuffle array **in-place**:

```python
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)

print(arr)
```

### 5. Random Permutation (returns new array)

```python
arr = np.random.permutation(10)
print(arr)
```

### 6. Random Seed (reproduce results)

To get **same random numbers every time**:

```python
np.random.seed(42)

print(np.random.rand(3))
```

If you run again with seed=42, output will match exactly.

### 7. Random distribution samples

#### Normal distribution with custom mean & std

```python
arr = np.random.normal(loc=50, scale=10, size=5)
print(arr)
```

#### Binomial distribution

```python
arr = np.random.binomial(n=10, p=0.5, size=5)
print(arr)
```

#### Poisson distribution

```python
arr = np.random.poisson(lam=4, size=5)
print(arr)
```

### 8. New Generator API (recommended)

NumPy recommends using:

```python
rng = np.random.default_rng()
```

Example:

```python
rng = np.random.default_rng()

print(rng.integers(0, 10, size=5))
print(rng.random(3))
```

### Summary Table

| Purpose | Function |
| --- | --- |
| Random integer | `randint`, `integers` |
| Random float 0–1 | `rand`, `random` |
| Normal distribution | `randn`, `normal` |
| Random choice from array | `choice` |
| Shuffle array | `shuffle`, `permutation` |
| Reproducibility | `seed` |
| Modern API | `default_rng()` |

## Answers

### Answer by Anubhav Sharma

## NumPy Random Numbers

[NumPy](https://www.mindstick.com/interview/34416/how-to-filter-an-array-using-numpy) provides random number functions through:

```plaintext
numpy.random
```

There are many ways to generate random numbers:

### 1. Random Integer (`randint`)

```python
import numpy as np

# Random integer between 0 and 10 (exclusive)
x = np.random.randint(0, 10)
print(x)
```

Generate **multiple** random integers:

```python
arr = np.random.randint(0, 100, size=5)
print(arr)
```

### 2. Random Floats (`rand`, `randn`, `random`)

#### Uniform distribution (0 → 1)

```python
x = np.random.rand()
print(x)
```

Multi-dimensional:

```python
arr = np.random.rand(3, 2)
print(arr)
```

#### Normal distribution (mean=0, std=1)

```python
x = np.random.randn(5)
print(x)
```

#### General random floats

```python
arr = np.random.random((2, 3))
print(arr)
```

### 3. Random Choice (`choice`)

Select random values from a list or array:

```python
items = np.array([10, 20, 30, 40, 50])

x = np.random.choice(items)
print(x)
```

Select multiple:

```python
arr = np.random.choice(items, size=3)
print(arr)
```

With replacement disabled:

```python
np.random.choice(items, size=3, replace=False)
```

### 4. Random Shuffle

Shuffle array **in-place**:

```python
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)

print(arr)
```

### 5. Random Permutation (returns new array)

```python
arr = np.random.permutation(10)
print(arr)
```

### 6. Random Seed (reproduce results)

To get **same random numbers every time**:

```python
np.random.seed(42)

print(np.random.rand(3))
```

If you run again with seed=42, output will match exactly.

### 7. Random distribution samples

#### Normal distribution with custom mean & std

```python
arr = np.random.normal(loc=50, scale=10, size=5)
print(arr)
```

#### Binomial distribution

```python
arr = np.random.binomial(n=10, p=0.5, size=5)
print(arr)
```

#### Poisson distribution

```python
arr = np.random.poisson(lam=4, size=5)
print(arr)
```

### 8. New Generator API (recommended)

NumPy recommends using:

```python
rng = np.random.default_rng()
```

Example:

```python
rng = np.random.default_rng()

print(rng.integers(0, 10, size=5))
print(rng.random(3))
```

### Summary Table

| Purpose | Function |
| --- | --- |
| Random integer | `randint`, `integers` |
| Random float 0–1 | `rand`, `random` |
| Normal distribution | `randn`, `normal` |
| Random choice from array | `choice` |
| Shuffle array | `shuffle`, `permutation` |
| Reproducibility | `seed` |
| Modern API | `default_rng()` |


---

Original Source: https://www.mindstick.com/interview/34417/how-to-use-random-numbers-in-numpy

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
