NumPy Permutations
A permutation rearranges an existing collection. It is useful for randomized processing order or assigning observations to groups without inventing new values.
How it works
Generator.permutation returns a shuffled copy for this one-dimensional fixture, while shuffle changes the supplied array in place. A permutation preserves multiplicity: repeated source values remain repeated. Random ordering alone does not make a dataset representative, remove confounding or establish a fair real-world assignment process. Keep the original identifiers so a selected order can still be audited.
Run the code
Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.
For local work, use a separate virtual environment and run python permutations.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import numpy as np
source = np.arange(8)
shuffled = np.random.default_rng(41).permutation(source)
print(np.array_equal(np.sort(shuffled), source))
print(source.tolist())
print(shuffled.shape)
Expected result
True [0, 1, 2, 3, 4, 5, 6, 7] (8,)
The sorted result recovers every original position and the input remains in its original order. The exact randomized order is intentionally not printed as a cross-version promise. The invariant captures the important contract.
Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.