Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Random Permutations

Random Permutations of Elements

A permutation refers to an arrangement of elements; for example, [3, 2, 1] is a permutation of [1, 2, 3] and vice versa.

The NumPy random module offers two methods for this: shuffle() and permutation().

Shuffling Arrays

Shuffling means altering the arrangement of elements in place, meaning it changes the order directly within the array itself.

Example

Randomly rearrange the elements of the following array:

from numpy import random
import numpy as np

arr = np.array([12345])

random.shuffle(arr)

print(arr)

The shuffle() method modifies the original array.

Generating Permutation of Arrays

Example

Create a random permutation of the elements in the following array:

from numpy import random
import numpy as np

arr = np.array([12345])

print(random.permutation(arr))