Skip to content

randomized-algorithm/random

Folders and files

NameName
Last commit message
Last commit date
Feb 20, 2025
Apr 6, 2021
Mar 9, 2022
Mar 3, 2024
Mar 3, 2024
Apr 6, 2021
Jul 15, 2021
Jul 15, 2021
Mar 9, 2022
Apr 6, 2021
Oct 2, 2014
Mar 3, 2024
May 10, 2025
Apr 26, 2021
Apr 6, 2021
May 10, 2025

Repository files navigation


© xkcd.com

Randomness algorithms for JavaScript. See docs. Parent is js-algorithms.

⚠️ Depending on your environment, the code may require regeneratorRuntime to be defined, for instance by importing regenerator-runtime/runtime.

import {
	randint , // randint(i, j) => [i, j[ \cap ZZ
	randfloat , // randfloat(i, j) => [i, j[
	sample , // sample(k, array, i, j) /!\ in-place (output is items i through i + k - 1)
	shuffle , // shuffle(array, i, j) /!\ in-place
	reservoir as sampled , // sampled(k, iterable[, output = []]) => sample array
	shuffled , // shuffled(iterable[, output = []]) => shuffled array
	choice , // choice(array, i, j) => item
	randrange , // randrange([start = 0,] stop[, step = 1]) => item
} from '@randomized/random' ;

// NOTE a specific entropy source can be used by constructing these functions
// from their low-level implementation, for instance

import {_fisheryates, _randint} from '@randomized/random';
import {splitmix64, nextFloat64} from '@entropy-source/pseudo-random';

const prng = splitmix64([123, 456]);
const random = () => nextFloat64(prng);
const randint = _randint(random);
const sample = _fisheryates(randint);

License Version Tests Dependencies GitHub issues Downloads

Code issues Code maintainability Code coverage (cov) Code technical debt Documentation Package size