The RandomList
class provides three static methods to handle a few random array operations.
import RandomList from "@n-eeraj/random/RandomList"
.choice(array)
Selects a random element from the provided array.
Example Usage:
const options = [1, 2, "abc", { x: 5 }]
RandomList.choice(options)
// Returns one random element from the options array
.shuffle(array, mixCount)
Returns a shuffled version of the provided array. Mix count is an optional argument, it dictates how much times the random swapping should take place.
Example Usage:
const array = [1, 2, "abc", { x: 5 }]
RandomList.shuffle(array, 3)
// Returns an array with same elements after shuffling random indices 3 times
.sample(array, sampleSize)
Returns a sample array (sub array) of the provided array of sampleSize length.
Example Usage:
const array = [1, 2, "abc", { x: 5 }]
RandomList.sample(array, 2)
// Returns an array with 2 random elements from the original array