Nodejs Array Random random()

Here you can find the source of random()

Method Source Code

// Returns a random element in the Array

Array.prototype.random = function() {

    // Prototypes throw TypeErrors when the context or arguments are invalid

    if (Object.prototype.toString.call(this) !== '[object Array]') {
        throw new TypeError("`this` must be Array, not " + typeof this);
    }// www . ja va  2s .com

    return this[Math.floor(Math.random() * this.length)];
};

Related

  1. random()
    Array.prototype.random = function() {
      return this[Math.floor(Math.random() * this.length)];
    };
    
  2. random()
    Array.prototype.random = function() {
        return this[~~(this.length * Math.random())];
    };
    function rand(n) {
        return ~~(Math.random() * n);
    
  3. random()
    Math.randomInt = function (min, max) {
        return Math.floor(Math.random() * (max - min +1)) + min
    };
    Array.prototype.random = function () {
       return this[Math.randomInt(0, this.length-1)];
    
  4. random()
    Array.prototype.random = function() {
      if (!this.length) return;
      return this[Math.floor(Math.random() * this.length)];
    };
    
  5. random()
    Array.prototype.random = function () {
      return this[Math.floor((Math.random()*this.length))];
    };
    
  6. random()
    Array.prototype.random = function() {
      return this[ Math.randomInt( 0, this.length - 1 ) ];
    };
    
  7. random()
    Array.prototype.random = function () {
        return this[ (Math.random() * this.length).floor() ];
    };
    
  8. random()
    Array.prototype.random = function() {
      if (!this.length) { return null; }
      return this[Math.floor(ROT.RNG.getUniform() * this.length)];
    
  9. random()
    Array.prototype.random = function(){
        return this[sb.math.rand(0,this.length)];
    };