Random Permutation
A permutation is a rearrangement of elements. [3, 1, 2] is a permutation of [1, 2, 3]. Random permutations are used to shuffle a deck of cards, randomise the order of quiz questions, or mix a dataset before splitting it into training and test sets.
A Generator offers two methods:
| Method | Changes the original? | Returns |
|---|---|---|
rng.shuffle(a) |
Yes, in place | None |
rng.permutation(a) |
No | a shuffled copy |
Shuffling in place
import numpy as np
rng = np.random.default_rng(2024)
playlist = np.array(["Intro", "Blue", "Echo", "Drift", "Finale"])
rng.shuffle(playlist)
print(playlist)['Drift' 'Finale' 'Blue' 'Echo' 'Intro']
A shuffled copy with permutation
permutation leaves the original untouched. Given an integer n, it returns a shuffled arange(n), a random ordering of indices:
import numpy as np
rng = np.random.default_rng(2024)
questions = np.array(["Q1", "Q2", "Q3", "Q4", "Q5"])
print(rng.permutation(questions))
print(questions) # still in the original order
print(rng.permutation(8)) # random order of 0..7['Q4' 'Q5' 'Q2' 'Q3' 'Q1']
['Q1' 'Q2' 'Q3' 'Q4' 'Q5']
[5 7 6 2 3 4 0 1]
Shuffling 2-D arrays
On a 2-D array, shuffle and permutation move whole rows (they shuffle along axis=0), keeping each row intact. That is exactly what you want for a dataset where each row is one record:
import numpy as np
rng = np.random.default_rng(5)
records = np.array([[1, 10, 100],
[2, 20, 200],
[3, 30, 300],
[4, 40, 400]])
print(rng.permutation(records)) # rows reordered, each row kept intact
print(rng.permutation(records, axis=1)) # columns reordered instead[[ 4 40 400]
[ 2 20 200]
[ 3 30 300]
[ 1 10 100]]
[[100 1 10]
[200 2 20]
[300 3 30]
[400 4 40]]
To shuffle elements independently within each row, use rng.permuted(a, axis=1), which treats every row separately.
Shuffling two arrays in the same order
Features and labels often live in separate arrays that must stay aligned. Shuffle a set of indices once, then apply it to both:
import numpy as np
rng = np.random.default_rng(99)
features = np.array([[5.1, 3.5], [6.2, 2.9], [4.7, 3.2], [7.0, 3.1], [5.5, 2.4]])
labels = np.array(["A", "B", "A", "B", "B"])
order = rng.permutation(len(labels))
features, labels = features[order], labels[order]
print(order)
for f, lab in zip(features, labels):
print(f, lab)[2 0 3 4 1]
[4.7 3.2] A
[5.1 3.5] A
[7. 3.1] B
[5.5 2.4] B
[6.2 2.9] B
Example: dealing cards
Shuffling a deck and dealing hands combines permutations with reshaping:
import numpy as np
rng = np.random.default_rng(8)
ranks = ["A", "K", "Q", "J", "10", "9", "8", "7"]
suits = ["♠", "♥"]
deck = np.array([r + s for s in suits for r in ranks]) # 16 cards
shuffled = rng.permutation(deck)
hands = shuffled[:12].reshape(3, 4) # 3 players, 4 cards each
for i, hand in enumerate(hands, start=1):
print(f"player {i}:", " ".join(hand))
print("left in deck:", len(shuffled) - 12)player 1: J♥ J♠ A♠ 7♠
player 2: 9♥ A♥ 10♥ K♠
player 3: 8♥ 8♠ 7♥ 9♠
left in deck: 4
Legacy note: older code uses
np.random.shuffle(a)andnp.random.permutation(a), which work the same way but on the hidden global generator seeded withnp.random.seed().
Test yourself
What is the difference between rng.shuffle(a) and rng.permutation(a)?
- shuffle returns a copy, permutation edits in place
- shuffle edits in place, permutation returns a shuffled copy
- There is no difference
- permutation only works on integers
What does rng.permutation(5) return?
- Five random floats
- A shuffled arrangement of 0, 1, 2, 3, 4
- A random integer below 5
- The number 120
How does rng.shuffle treat a 2-D array by default?
- Shuffles every element independently
- Shuffles the rows, keeping each row intact
- Shuffles the columns
- Raises an error