🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.random.permutation()

AI & DATA SCIENCE // np-random-permutation

np.random.permutation() returns a new, randomly shuffled copy of a sequence, or a randomly shuffled range of integers, leaving any original array input untouched.

Syntax

np.random.permutation(x)

Deep Dive Course

Unlike shuffle(), which mutates its argument in place and returns nothing, permutation() always returns a brand-new shuffled array and leaves the original input completely unmodified — the safer, non-destructive counterpart to shuffle(). Passing an integer n is shorthand for shuffling a range of n integers, a common way to generate a random ordering of indices without first building the range array yourself.

1Understanding np.random.permutation()

Unlike shuffle(), which mutates its argument in place and returns nothing, permutation() always returns a brand-new shuffled array and leaves the original input completely unmodified — the safer, non-destructive counterpart to shuffle(). Passing an integer n is shorthand for shuffling a range of n integers, a common way to generate a random ordering of indices without first building the range array yourself.

💡

Use permutation() instead of shuffle() specifically when you need to keep the original, unshuffled array around — shuffle() would destroy that original ordering permanently by mutating in place.

editor.html
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
shuffled = np.random.permutation(arr)
print(shuffled)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.random.permutation() showing how it is used in production NumPy code.

editor.html
import numpy as np

np.random.seed(0)
indices = np.random.permutation(5)
print(indices)
localhost:3000

3Best Practices

Follow these guidelines when working with np.random.permutation():

1. Use permutation() over shuffle() whenever the original, unshuffled array needs to remain available afterward

2. Pass an integer directly to permutation() as a convenient shorthand for generating a random ordering of indices, instead of building the range array first

3. Use a permutation of indices to shuffle several related arrays in the same consistent random order, by applying the same generated index permutation to each one

⚠️

Tip: Use permutation() instead of shuffle() specifically when you need to keep the original, unshuffled array around — shuffle() would destroy that original ordering permanently by mutating in place.

editor.html
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
shuffled = np.random.permutation(arr)
print(shuffled)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
shuffled = np.random.permutation(arr)
print(shuffled)
print(arr)
Example 02Advanced Example
import numpy as np

np.random.seed(0)
indices = np.random.permutation(5)
print(indices)

Best Practices

  • Use permutation() over shuffle() whenever the original, unshuffled array needs to remain available afterward
  • Pass an integer directly to permutation() as a convenient shorthand for generating a random ordering of indices, instead of building the range array first
  • Use a permutation of indices to shuffle several related arrays in the same consistent random order, by applying the same generated index permutation to each one

Interview Question

Why is permutation() generally safer to use than shuffle() when the original data still needs to be used afterward?

Hint: Think about which one mutates and which one copies.

shuffle() mutates its input array in place and returns nothing, permanently discarding the original ordering the moment it runs — if that original order is still needed anywhere else, it's already gone. permutation() instead always produces a brand-new shuffled array as its return value while leaving the original input completely untouched, so you can keep both the original and the shuffled version available side by side, making it the safer default whenever you're not certain the original order can be safely discarded.

Exercises

MediumPractice using np.random.permutation() in a real scenario.
View Solution
import numpy as np

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

Frequently Asked Questions

Why is permutation() generally safer to use than shuffle() when the original data still needs to be used afterward?

shuffle() mutates its input array in place and returns nothing, permanently discarding the original ordering the moment it runs — if that original order is still needed anywhere else, it's already gone. permutation() instead always produces a brand-new shuffled array as its return value while leaving the original input completely untouched, so you can keep both the original and the shuffled version available side by side, making it the safer default whenever you're not certain the original order can be safely discarded.

Related Functions

np-random-shufflenp-random-choicenp-unique