Open In App

numpy.random.permutation() in Python

Last Updated : 22 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

numpy.random.permutation() function in the NumPy library that returns a new array with the elements randomly permuted, leaving the original array unchanged. It can be used with sequences or integers.

Example:

Python
import numpy as np

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

Output
[1 5 4 2 3]
[1 2 3 4 5]

Explanation: The returned array is shuffled, but the original remains unchanged.

Syntax

numpy.random.permutation(x)

Parameter: x can be an integer or array-like (list, NumPy array, etc.).

  • If x is an integer n, it returns a shuffled array of numbers [0, 1, ..., n-1].
  • If x is an array, it returns a shuffled copy of the array.

Returns: A new array with permuted values.

Examples

Example 1: Permutation with an integer

Python
import numpy as np

res = np.random.permutation(5)
print(res)

Output
[3 2 1 4 0]

Explanation: Returns a shuffled array of integers from 0 to n-1.

Example 2: Working with 2D arrays

Python
import numpy as np

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

Output
[[1 2]
 [3 4]
 [5 6]]

Explanation: The function permutes the rows (axis 0) of the 2D array.

Example 3: Attempting in-place modification

Python
import numpy as np

arr = np.array([1, 2, 3])
np.random.permutation(arr)
print(arr)

Output
[1 2 3]

Explanation: The original array is unchanged. permutation() does not modify it in-place.


Practice Tags :

Similar Reads