Skip to content

randomized-algorithm/random

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