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.
20 lines (19 loc) • 536 B
text/typescript
var Alea = require('alea');
export const arrayPick = (array: any[]) => {
if (array) {
if (Array.isArray(array)) {
const prng = new Alea();
if (array.length < 1 || !array) {
return null;
} else if (array.length === 1) {
return array[0];
} else {
return array[Math.round(prng() * (array.length - 1))];
}
} else {
return array;
}
} else {
return null;
}
}