forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveRandomElement.js
More file actions
29 lines (24 loc) · 1.03 KB
/
Copy pathRemoveRandomElement.js
File metadata and controls
29 lines (24 loc) · 1.03 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
var SpliceOne = require('./SpliceOne');
// Removes a random object from the given array and returns it.
// Will return null if there are no array items that fall within the specified range
// or if there is no item for the randomly chosen index.
/**
* [description]
*
* @function Phaser.Utils.Array.RemoveRandomElement
* @since 3.0.0
*
* @param {array} array - The array to removed a random element from.
* @param {integer} [start=0] - The array index to start the search from.
* @param {integer} [length=array.length] - Optional restriction on the number of elements to randomly select from.
*
* @return {object} The random element that was removed, or `null` if there were no array elements that fell within the given range.
*/
var RemoveRandomElement = function (array, start, length)
{
if (start === undefined) { start = 0; }
if (length === undefined) { length = array.length; }
var randomIndex = start + Math.floor(Math.random() * length);
return SpliceOne(array, randomIndex);
};
module.exports = RemoveRandomElement;