UNPKG

1.36 kBJavaScriptView Raw
1/** @module splat-ecs/lib/random */
2
3module.exports = {
4 /**
5 * Get a pseudo-random number between the minimum (inclusive) and maximum (exclusive) parameters.
6 * @function inRange
7 * @param {number} min Inclusive minimum value for the random number
8 * @param {number} max Exclusive maximum value for the random number
9 * @returns {number} A number between <code>min</code> and <code>max</code>
10 * @see [Bracket Notation: Inclusion and Exclusion]{@link https://en.wikipedia.org/wiki/Bracket_%28mathematics%29#Intervals}
11 * @example
12var random = require("splat-ecs/lib/random");
13random.inRange(0, 1) // Returns 0.345822917402371
14random.inRange(10, 100) // Returns 42.4823819274931274
15 */
16 "inRange": function(min, max) {
17 return min + Math.random() * (max - min);
18 },
19
20 /**
21 * Get a random element in an array
22 * @function from
23 * @param {array} array Array of elements to choose from
24 * @returns {Object} A random element from the given array
25 * @example
26var random = require("splat-ecs/lib/random");
27var fruit = ["Apple", "Banana", "Orange", "Peach"];
28random.from(fruit); // Could return "Orange"
29random.from(fruit); // Could return "Apple"
30random.from(fruit); // Could return "Peach"
31random.from(fruit); // Could return "Banana"
32 */
33 "from": function(array) {
34 return array[Math.floor(Math.random() * array.length)];
35 }
36};