Nodejs Array Random randomize()

Here you can find the source of randomize()

Method Source Code

function range(from, to) {
  var result = []
  for(var i = from; i <= to; i++) {
    result.push(i)/*from   www.j ava 2s  . co  m*/
  }
  return result
}

Array.prototype.randomize = function() {
  this.sort(function(){ return (Math.round(Math.random()) - 0.5) })
  return this
}

Array.prototype.toStringWithoutBrackets = Array.prototype.toString
Array.prototype.toString = function() {
  return '['+this.toStringWithoutBrackets()+']'
}

Array.prototype.swap = function(a, b) {
  var tmp = this[a]
  this[a] = this[b]
  this[b] = tmp
}

Array.prototype.first = function() {
  if(this.length > 0) {
    return this[0]  
  } else {
    return null
  }
}

Array.prototype.rest = function() {
  if(this.length > 0) {
    return this.slice(1)
  } else {
    return []
  }
}

Array.prototype.last = function() {
  if(this.length > 0) {
    return this[this.length - 1]  
  } else {
    return null
  }
}

Array.prototype.append = function(array) {
  this.push.apply(this, array)
  return this
}

Related

  1. random_item()
    'use strict';
    Array.prototype.random_item = function () {
        return this[Math.floor(Math.random() * this.length)];
    };
    var printError = function (msg) {
        var newline = document.createElement('br');
        var content = document.createTextNode(msg);
        var messages = document.getElementById('messages');
        messages.appendChild(content);
    ...
    
  2. randomise()
    Array.prototype.randomise = function() {
        return this.sort(function() { return 0.5 - Math.random() });
    
  3. randomize()
    Array.prototype.randomize = function() {
      var result = [];
      while (this.length) {
        var index = this.indexOf(this.random());
        result.push(this.splice(index, 1)[0]);
      return result;
    
  4. randomize()
    Array.prototype.randomize = Array.prototype.randomize || function() {
      var result = [];
      var clone = this.slice();
      while (clone.length) {
        var index = clone.indexOf(clone.random());
        result.push(clone.splice(index, 1)[0]);
      return result;
    
  5. randomize()
    Array.prototype.randomize = function() {
        if (this.length < 2) {
            return this;
        var items = Array.from(this);
        var newArr = [];
        while (items.length > 0) {
            var entry = items.random();
            newArr.push(entry);
    ...
    
  6. randomize()
    Array.prototype.randomize = function(){
      var size = this.length;
      var itensInArray = 0;
      var newArray = [];
      var emptyObject = {}; 
      this.forEach(function(){ newArray.push(emptyObject); }); 
      this.forEach(function(item){
        var index = Math.randomInt(0, size - 1);
        while(newArray[index] != emptyObject) {
    ...
    
  7. randomize()
    Array.prototype.randomize = function() {
      this.sort(function() {
        return Math.floor(Math.random() * 2) ? -1 : 1;
      });
      return this;
    
  8. randomize()
    Array.prototype.randomize = function()
      var i = this.length, j, temp;
      while ( --i )
        j = Math.floor( Math.random() * (i - 1) );
        temp = this[i];
        this[i] = this[j];
        this[j] = temp;
    ...