Shuffle arrays and typed arrays in place or as a copy, with a custom random source for seeded shuffles.
npm install typed-array-shuffle
import { shuffle, toShuffled } from "typed-array-shuffle"; import random from "pex-random"; // In place const indices = shuffle(Uint32Array.from({ length: 10 }, (_, i) => i)); // Copy, keeping the typed array type const values = toShuffled(Float32Array.of(0.1, 0.2, 0.3)); // Seeded, with any source of numbers in [0, 1) random.seed(1); const seeded = shuffle([1, 2, 3, 4], () => random.float());
- typedArrayShuffle
- static
- .shuffle(array, [random]) ⇒
T - .toShuffled(collection, [random]) ⇒
Array.<T>|ArrayLike.<T>
- .shuffle(array, [random]) ⇒
- inner
- ~RandomSource :
function
- ~RandomSource :
- static
Shuffle an array or typed array in place, uniformly.
Kind: static method of typedArrayShuffle
Returns: T - The shuffled array.
| Param | Type | Default |
|---|---|---|
| array | T |
|
| [random] | RandomSource |
Math.random |
Shuffled copy of an array, typed array or iterable. Typed arrays keep their type, anything else becomes an array.
Kind: static method of typedArrayShuffle
| Param | Type | Default |
|---|---|---|
| collection | ArrayLike.<T> | Iterable.<T> |
|
| [random] | RandomSource |
Math.random |
Returns a number in [0, 1).
Kind: inner typedef of typedArrayShuffle
MIT. See license file.