deep-random-pick
Version:
Picks a pseudo-random item from a pool in several ways, including a recursive method that allows for more control over probability.
21 lines (17 loc) • 492 B
text/typescript
import { shallowPick } from './shallowpick';
export const randomPick = (...pool) => {
const itemPicked = shallowPick(pool);
if (Array.isArray(itemPicked) && itemPicked.length > 0) {
// Recursion is required here.
return randomPick(itemPicked);
} else {
return itemPicked;
}
};
/*
exports.deepRandomPick = (...pool) => {
return randomPick(pool);
};
exports.shallowPick = (...pool) => { return shallowPick() };
*/