1 | /** @module splat-ecs/lib/random */
|
2 |
|
3 | module.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
|
12 | var random = require("splat-ecs/lib/random");
|
13 | random.inRange(0, 1) // Returns 0.345822917402371
|
14 | random.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
|
26 | var random = require("splat-ecs/lib/random");
|
27 | var fruit = ["Apple", "Banana", "Orange", "Peach"];
|
28 | random.from(fruit); // Could return "Orange"
|
29 | random.from(fruit); // Could return "Apple"
|
30 | random.from(fruit); // Could return "Peach"
|
31 | random.from(fruit); // Could return "Banana"
|
32 | */
|
33 | "from": function(array) {
|
34 | return array[Math.floor(Math.random() * array.length)];
|
35 | }
|
36 | };
|