Nodejs Array Random random()

Here you can find the source of random()

Method Source Code

window.console = window.console || {'log':function(){}}

//random int between min (included) and max (included)
function randomInt(min, max) {
   return Math.floor(Math.random() * (max - min + 1)) + min;
}

//get random element from array
Array.prototype.random = function () {
   return this[Math.random() * this.length - 1 | 0];
};

///*w w w . j  av a  2  s . c  o  m*/
Array.prototype.subRandom = function () {
   var length = randomInt(0, this.length)
   var subArray = []
   for (i=0; i<length; i++) {
      subArray.push(this.random())
   }
   return _.uniq(subArray);
};



//unorder array
Array.prototype.shuffle = function () {
   for (var i = this.length; i > 0; --i)
      this.push(this.splice(Math.random() * i | 0, 1)[0]);
   return this;
};

//Add composition. see http://scott.sauyet.com/Javascript/Talk/Compose/2013-05-22/#slide-15
Function.prototype.compose = function(g) {
   var fn = this;
   return function() {
      return fn.call(this, g.apply(this, arguments));
   };
};

Related

  1. random()
    Array.prototype.random = function() {
      return this[Math.floor((this.length-1)*Math.random())];
    };
    
  2. random()
    function XHR(method, path, data, callback) {
      var xhr = new XMLHttpRequest();
      xhr.open(method, path, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState != 4) return;
        callback(xhr.status, xhr.responseText);
      xhr.send(data);
    function getPos(obj) {
      var curleft = 0, curtop = 0;
      if (obj.offsetParent)
        do {
          curleft += obj.offsetLeft;
          curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
      return {x: curleft, y: curtop};
    function toRangeOrMiddle(a, x ,b) {
      if (b < a) return (a+b)/2;
      if (x < a) return a;
      if (x > b) return b;
      return x;
    Array.prototype.random = function() {
      return this[Math.random()*this.length|0];
    window.onerror = function(errorMsg, url, lineNumber) {
      var msg = "Error happened on <"+url+
        "\n> on line "+lineNumber+":\n"+
        errorMsg;
      console.log(msg);
    
  3. random()
    Math.randomInt = function (min, max) {
        return Math.round(Math.random() * (max - min) + min);
    };
    Array.prototype.random = function () {
        this[Math.randomInt(0, this.length-1)];
    
  4. random()
    Array.prototype.random = function () { return (this[(Math.max(Math.random() * this.length - 1, 0).toFixed())]) } 
    Array.prototype.fillNull = function(){this[].fill(function(){return null})}
    Array.prototype.bool = function () { return this.map(function (v) { return v ? true : false }) } 
    Array.prototype.a01 = function () { return this.map(function (v) { return v ? 1 : 0 }) }
    Array.prototype.splitToChunks = function (num) {
      var ret = []
      for (var i = 0; i < this.length / num; i++) {
        ret.push(this.slice(i * num, i * num + num))
      return ret
    Array.prototype.removeByIndex = function (index) {
      return this.splice(index, 1)
    Math.myRandom = function (max, min) {
      if(max == undefined) return Math.random()
      if(min == undefined) return Math.random() * max;
      return Math.random() * (max+-min) + min;
    
  5. random()
    Array.prototype.random = function () {
      var i, j, k, ti, tj;
      for (k = 0; k < this.length; k++) {
        i = this.length;
        while (--i) {
          j = Math.floor(Math.random() * (i - 1));
          ti = this[i];
          tj = this[j];
          this[j] = ti;
    ...
    
  6. random()
    Array.prototype.random = function() {
      return this[Math.floor(Math.random() * this.length)];
    Number.prototype.toPrice = function() {
      return this.valueOf().toFixed(2).replace(/./g, function(c, i, a) {
        return i && c !== "." && !((a.length - i) % 3) ? "," + c : c;
      });
    
  7. random()
    Array.prototype.random = function(){
      return this[Math.round(Math.random()*(this.length-1))]
    
  8. random()
    Array.prototype.random = function()
      return this[Random(0, this.length - 1)];
    
  9. random()
    Array.prototype.random = function() {
      if (this.length == 0) return void 0;
      return this[Math.floor(Math.random() * this.length)];
    };