forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetRandom.js
More file actions
29 lines (25 loc) · 1.01 KB
/
Copy pathGetRandom.js
File metadata and controls
29 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Returns a Random element from the array.
*
* @function Phaser.Utils.Array.GetRandom
* @since 3.0.0
*
* @param {array} array - The array to select the random entry from.
* @param {integer} [startIndex=0] - An optional start index.
* @param {integer} [length=array.length] - An optional length, the total number of elements (from the startIndex) to choose from.
*
* @return {*} A random element from the array, or `null` if no element could be found in the range given.
*/
var GetRandom = function (array, startIndex, length)
{
if (startIndex === undefined) { startIndex = 0; }
if (length === undefined) { length = array.length; }
var randomIndex = startIndex + Math.floor(Math.random() * length);
return (array[randomIndex] === undefined) ? null : array[randomIndex];
};
module.exports = GetRandom;