@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
20 lines (15 loc) • 633 B
JavaScript
import { assert } from "../../assert.js";
import { randomIntegerBetween } from "./randomIntegerBetween.js";
/**
* @template T
* @param {function} random if you don't have a custom RNG, you can always pass {@link Math.random} instead
* @param {T[]} array
* @returns {T|undefined} returns undefined only if that value is in the array or if array is empty
*/
export function randomFromArray(random, array) {
assert.isArray(array, 'array');
assert.isFunction(random, 'random');
const length = array.length;
const index = randomIntegerBetween(random, 0, length - 1);
return array[index];
}