---
title: "Random Permutations using Numpy."  
description: "Random Permutations using Numpy."  
author: "Ravi Vishwakarma"  
published: 2025-11-26  
updated: 2025-11-26  
canonical: https://www.mindstick.com/interview/34418/random-permutations-using-numpy  
category: "python"  
tags: ["python-3.4", "python", "numpy"]  
reading_time: 3 minutes  

---

# Random Permutations using Numpy.

> **Random Permutations** in NumPy allow you to **shuffle** data or generate a **random ordering** of elements.

NumPy provides two important functions for this:

## 1. `numpy.random.permutation()`

Creates a **new shuffled copy** of a sequence or array.

### Example 1: Shuffle numbers 0 to 9

```python
import numpy as np

print(np.random.permutation(10))
```

Output example:

```plaintext
[3 7 1 0 6 9 5 4 2 8]
```

### Example 2: Shuffle an existing array

```python
arr = np.array([10, 20, 30, 40])
print(np.random.permutation(arr))
```

Output:

```plaintext
[30 10 40 20]
```

## 2. `numpy.random.shuffle()`

**Shuffles the original array in-place** (modifies the existing one).

### Example:

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

Output (original array is changed):

```plaintext
[3 1 4 2]
```

## Difference Between permutation() and shuffle()

| Function | Returns New Array? | Modifies Original? |
| --- | --- | --- |
| `np.random.permutation()` | Yes | No |
| `np.random.shuffle()` | No | Yes |

## Permuting Multi-Dimensional Arrays

### permutation() always shuffles rows:

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

Possible output:

```plaintext
[[5 6]
 [1 2]
 [3 4]]
```

## Randomly Shuffle List (Python list)

NumPy also works with Python lists:

```python
print(np.random.permutation([1, 2, 3, 4, 5]))
```

## Summary

- **Random permutation** = random ordering of elements
- Use `np.random.permutation()` when you want a **new shuffled copy**
- Use `np.random.shuffle()` when you want to **shuffle in-place**

## Answers

### Answer by Ravi Vishwakarma

> **Random Permutations** in NumPy allow you to **shuffle** data or generate a **random ordering** of elements.

NumPy provides two important functions for this:

## 1. `numpy.random.permutation()`

Creates a **new shuffled copy** of a sequence or array.

### Example 1: Shuffle numbers 0 to 9

```python
import numpy as np

print(np.random.permutation(10))
```

Output example:

```plaintext
[3 7 1 0 6 9 5 4 2 8]
```

### Example 2: Shuffle an existing array

```python
arr = np.array([10, 20, 30, 40])
print(np.random.permutation(arr))
```

Output:

```plaintext
[30 10 40 20]
```

## 2. `numpy.random.shuffle()`

**Shuffles the original array in-place** (modifies the existing one).

### Example:

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

Output (original array is changed):

```plaintext
[3 1 4 2]
```

## Difference Between permutation() and shuffle()

| Function | Returns New Array? | Modifies Original? |
| --- | --- | --- |
| `np.random.permutation()` | Yes | No |
| `np.random.shuffle()` | No | Yes |

## Permuting Multi-Dimensional Arrays

### permutation() always shuffles rows:

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

Possible output:

```plaintext
[[5 6]
 [1 2]
 [3 4]]
```

## Randomly Shuffle List (Python list)

NumPy also works with Python lists:

```python
print(np.random.permutation([1, 2, 3, 4, 5]))
```

## Summary

- **Random permutation** = random ordering of elements
- Use `np.random.permutation()` when you want a **new shuffled copy**
- Use `np.random.shuffle()` when you want to **shuffle in-place**


---

Original Source: https://www.mindstick.com/interview/34418/random-permutations-using-numpy

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
