Nodejs Array Random randomize()

Here you can find the source of randomize()

Method Source Code

Array.prototype.randomize = function(){

   var size = this.length;
   var itensInArray = 0;
   var newArray = [];
   var emptyObject = {}; //used to mark unique replaceable space

   this.forEach(function(){ newArray.push(emptyObject); }); //create a new array with same size and fill with emptyObject

   this.forEach(function(item){

      var index = Math.randomInt(0, size - 1);
      //from  w  ww  . j av  a 2  s.c o  m
      while(newArray[index] != emptyObject) {
         index += 1;
         index %= size;
      }

      itensInArray++;
      newArray[index] = item;

   });

   return newArray;
}

Array.prototype.contains = function(element) {
   for(var i = 0; i < this.length; i++){
    if( element === this[i] ) return true;
   }
   return false;
};

Related

  1. randomise()
    Array.prototype.randomise = function() {
        return this.sort(function() { return 0.5 - Math.random() });
    
  2. 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;
    
  3. 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;
    
  4. 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);
    ...
    
  5. randomize()
    function range(from, to) {
      var result = []
      for(var i = from; i <= to; i++) {
        result.push(i)
      return result
    Array.prototype.randomize = function() {
      this.sort(function(){ return (Math.round(Math.random()) - 0.5) })
    ...
    
  6. randomize()
    Array.prototype.randomize = function() {
      this.sort(function() {
        return Math.floor(Math.random() * 2) ? -1 : 1;
      });
      return this;
    
  7. 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;
    ...