typed-array-shuffle
v1.0.0
Published
Shuffle arrays and typed arrays in place or as a copy, with a custom random source for seeded shuffles.
Maintainers
Readme
typed-array-shuffle
Shuffle arrays and typed arrays in place or as a copy, with a custom random source for seeded shuffles.
Installation
npm install typed-array-shuffleUsage
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());API
typedArrayShuffle
- typedArrayShuffle
- static
- .shuffle(array, [random]) ⇒ T
- .toShuffled(collection, [random]) ⇒ Array.<T> | ArrayLike.<T>
- inner
- ~RandomSource : function
- static
typedArrayShuffle.shuffle(array, [random]) ⇒ T
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 |
typedArrayShuffle.toShuffled(collection, [random]) ⇒ Array.<T> | ArrayLike.<T>
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 |
typedArrayShuffle~RandomSource : function
Returns a number in [0, 1).
Kind: inner typedef of typedArrayShuffle
License
MIT. See license file.
