Skip to content

shuffle

Randomly shuffle an array

197 bytes

Usage

Create a new array with the items of the given array but in a random order. The randomization is done using the Fisher-Yates algorithm, which is mathematically proven to be unbiased (i.e. all permutations are equally likely).

import * as _ from 'radashi'
const fish = [
{
name: 'Marlin',
weight: 105,
source: 'ocean',
},
{
name: 'Salmon',
weight: 22,
source: 'river',
},
{
name: 'Salmon',
weight: 22,
source: 'river',
},
]
_.shuffle(fish)

You can provide a custom random function to make the shuffle more or less random. The custom random function takes minimum and maximum values and returns a random number between them.

const array = [1, 2, 3, 4, 5]
const customRandom = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min
_.shuffle(array, customRandom)