Skip to content

shuffle

Randomly shuffle an array

190 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
import _
_
from 'radashi'
const
const fish: {
name: string;
weight: number;
source: string;
}[]
fish
= [
{
name: string
name
: 'Marlin',
weight: number
weight
: 105,
source: string
source
: 'ocean',
},
{
name: string
name
: 'Salmon',
weight: number
weight
: 22,
source: string
source
: 'river',
},
{
name: string
name
: 'Salmon',
weight: number
weight
: 22,
source: string
source
: 'river',
},
]
import _
_
.
shuffle<{
name: string;
weight: number;
source: string;
}>(array: readonly {
name: string;
weight: number;
source: string;
}[], random?: (min: number, max: number) => number): {
name: string;
weight: number;
source: string;
}[]
export shuffle

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).

@seehttps://radashi.js.org/reference/random/shuffle

@example

const numbers = [1, 2, 3, 4, 5]
const shuffled = shuffle(numbers)
// => [2, 1, 4, 5, 3]
shuffled !== numbers
// => true

@version12.1.0

shuffle
(
const fish: {
name: string;
weight: number;
source: string;
}[]
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
const array: number[]
array
= [1, 2, 3, 4, 5]
const
const customRandom: (min: number, max: number) => number
customRandom
= (
min: number
min
: number,
max: number
max
: number) =>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.floor(x: number): number

Returns the greatest integer less than or equal to its numeric argument.

@paramx A numeric expression.

floor
(
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.random(): number

Returns a pseudorandom number between 0 and 1.

random
() * (
max: number
max
-
min: number
min
+ 1)) +
min: number
min
import _
_
.
shuffle<number>(array: readonly number[], random?: (min: number, max: number) => number): number[]
export shuffle

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).

@seehttps://radashi.js.org/reference/random/shuffle

@example

const numbers = [1, 2, 3, 4, 5]
const shuffled = shuffle(numbers)
// => [2, 1, 4, 5, 3]
shuffled !== numbers
// => true

@version12.1.0

shuffle
(
const array: number[]
array
,
const customRandom: (min: number, max: number) => number
customRandom
)