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 means altering the arrangement of elements in place, meaning it changes the order directly within the array itself.
Randomly rearrange the elements of the following array:
from numpy import random import numpy as np arr = np.array([1, 2, 3, 4, 5]) random.shuffle(arr) print(arr) |
The shuffle()
method modifies the original array.
Create a random permutation of the elements in the following array:
from numpy import random import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(random.permutation(arr)) |