array-shuffle
Randomize the order of items in an array
Last updated 12 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install array-shuffle 
SYNC missed versions from official npm registry.

array-shuffle

Randomize the order of items in an array

Uses the Durstenfeld algorithm which is based on the Fisher–Yates algorithm.

Install

npm install array-shuffle

Usage

import {arrayToShuffled, arrayShuffle} from 'array-shuffle';

// Create a new shuffled array
const shuffled = arrayToShuffled([1, 2, 3, 4, 5, 6]);
//=> [3, 5, 4, 1, 2, 6]

// Shuffle in-place
const array = [1, 2, 3, 4, 5, 6];
arrayShuffle(array);
console.log(array);
//=> [3, 5, 4, 1, 2, 6]

API

arrayToShuffled(array, options?)

Create a new array with the items randomized (does not mutate the original array).

Returns a new array with the items randomized.

array

Type: Array

The array to shuffle.

options

Type: object

randomNumberGenerator

Type: () => number

Custom random number generator that returns a float between 0 (inclusive) and 1 (exclusive). This is useful for deterministic shuffling when you use a seeded generator.

import {arrayToShuffled} from 'array-shuffle';

const randomNumberGenerator = () => 0;
const deterministic = arrayToShuffled([1, 2, 3, 4, 5, 6], {randomNumberGenerator});
//=> [2, 3, 4, 5, 6, 1]

arrayShuffle(array, options?)

Randomize the order of items in an array, mutating the array in-place.

Returns the input array shuffled.

array

Type: Array

The array to shuffle.

options

Type: object

randomNumberGenerator

Type: () => number

Custom random number generator that returns a float between 0 (inclusive) and 1 (exclusive). This is useful for deterministic shuffling when you use a seeded generator.

Current Tags

  • 4.1.0                                ...           latest (2 months ago)

7 Versions

  • 4.1.0                                ...           2 months ago
  • 4.0.0                                ...           7 months ago
  • 3.0.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |