Randomly picked item from array, null when length=0 - Node.js Array

Node.js examples for Array:Random Array

Description

Randomly picked item from array, null when length=0

Demo Code

/**/*from www. j  a va  2 s. c o m*/
 * @returns {any} Randomly picked item, null when length=0
 */
Array.prototype.random = function() {
  if (!this.length) { return null; }
  return this[Math.floor(ROT.RNG.getUniform() * this.length)];
}

Related Tutorials