Javascript Array shuffle()
method
Array.prototype.shuffle = function(){ //v1.0 for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this;/*from w ww . j a v a2 s .c o m*/ };
Array.prototype.shuffle = function () { return this.sort((a, b) => Math.random() > 0.5 ? 1 : -1) }
Array.prototype.shuffle = function() { var i = this.length, j, temp; while ( --i )/*w ww .j a va 2 s . com*/ { j = Math.floor( Math.random() * (i - 1) ); temp = this[i]; this[i] = this[j]; this[j] = temp; } }
Array.prototype.shuffle = function () { for (var i = this.length, j; j = Math.floor(Math.random() * i), i --; ) this[i] = [this[j], this[j] = this[i]][0]; };
Array.prototype.shuffle = function() { var i = this.length, j, tempi, tempj; if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); tempi = this[i];//from w w w. j a va 2 s . com tempj = this[j]; this[i] = tempj; this[j] = tempi; } return this; }
Array.prototype.shuffle = function() { var i = this.length, j, temp; while ( --i > 0 ) { j = Math.floor( Math.random() * ( i +1 )); temp = this [j];//from ww w . j a va 2 s . c o m this [j] = this [i]; this [i] = temp; } return this; }; var array =[ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ]; var result = array.shuffle(); console.log( result );
// http://stackoverflow.com/a/6274398/450164 Array.prototype.shuffle = function () { for (var i = this.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = this[i]; this[i] = this[j];// w ww .j a va 2s . c o m this[j] = tmp; } return this; }
// Array shuffle/*from w w w . j a v a 2s . c om*/ Array.prototype.shuffle = function() { for (var i=this.length-1; i>0; i--) { var j = Math.floor(Math.random()*(i+1)); var tmp = this[i]; this[i] = this[j]; this[j] = tmp; } return this; }
Array.prototype.shuffle = function() { var i = this.length, j, temp; if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[ i ];// w ww .j av a2s . c o m this[ i ] = this[ j ]; this[ j ] = temp; } return this; }
Array.prototype.shuffle = function() { var a = this; var j, x, i;//from w w w . j ava2 s . c om for (i = a.length; i; i -= 1) { j = Math.floor(Math.random() * i); x = a[i - 1]; a[i - 1] = a[j]; a[j] = x; } return a; };
/**//from w ww .j a v a 2 s .c om * Remove accents from string. * * @method shuffle * @return {Array} Returns the array in a shuffle order */ Array.prototype.shuffle = function () { for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this; };
Array.prototype.shuffle = function() { var i = this.length, j, temp; if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];// ww w . j a va 2 s. c o m this[i] = this[j]; this[j] = temp; } return this; }
Array.prototype.shuffle = function() { var m = this.length, t, i; // While there remain elements to shuffle? while (m) {/* ww w. j a va 2 s. co m*/ // Pick a remaining element? i = Math.floor(Math.random() * m--); // And swap it with the current element. t = this[m]; this[m] = this[i]; this[i] = t; } return this; }
Array.prototype.shuffle = function() { var l = this.length + 1; while (l--) {//from w w w . j a v a 2 s .c o m var r = ~~(Math.random() * l); var o = this[r]; this[r] = this[0]; this[0] = o; } return this; };
Array.prototype.shuffle = function () { var i = this.length; while (1 < i--) { // generate random integer in the range [0,i] var j = Math.floor(Math.random() * (i+1)); var a_j = this[j]; this[j] = this[i];/* ww w . j ava 2 s . co m*/ this[i] = a_j; } };
/*/*from w ww. ja va2s. c o m*/ * Shuffles list in-place */ Array.prototype.shuffle = function() { var i, j, t; for (i = 1; i < this.length; i++) { j = Math.floor(Math.random()*(1+i)); // choose j in [0..i] if (j != i) { t = this[i]; // swap list[i] and list[j] this[i] = this[j]; this[j] = t; } } }
Array.prototype.shuffle = function() { this.sort(function() { return Math.random() - 0.5; });//from ww w . j ava2 s .c om };
// http://bost.ocks.org/mike/shuffle/ Array.prototype.shuffle = function(){ var m = this.length, t, i; // While there remain elements to shuffle? while (m) {//from w w w. ja v a2 s .c o m // Pick a remaining element? i = Math.floor(Math.random() * m--); // And swap it with the current element. t = this[m]; this[m] = this[i]; this[i] = t; } return this; };
Array.prototype.shuffle = function (){ var i = this.length, j, temp; if ( i == 0 ) return; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];//from w w w . j a va2 s . c om this[i] = this[j]; this[j] = temp; } };
Array.prototype.shuffle = function () { var rand, value, i; if (!this || this.length === 0) { return this; } for (i = 0; i < this.length; i++) { rand = Math.floor(Math.random() * i); value = this[i];//from w w w .ja v a2 s . c o m this[i] = this[rand]; this[rand] = value; } return this; };
/* /*from w w w.j av a2 s . c o m*/ To test: var array = [1,2,3,4,5,6,7,8,9]; console.log(array.shuffle()); // something like this [1, 2, 6, 4, 3, 5, 9, 8, 7] */ Array.prototype.shuffle = function() { var array = Object(this); return array.sort(function() { return Math.random() - 0.5; }); };
Array.prototype.shuffle = function() { var arr = this, elm, inc, length = arr.length; while (length) { inc = Math.floor(Math.random() * length--); elm = arr[length];/*from w w w.ja v a 2 s . c o m*/ arr[length] = arr[inc]; arr[inc] = elm; } return arr; };
Array.prototype.shuffle = function (){ var i = this.length, j, temp; if ( i == 0 ) return; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];//from w ww . j a v a 2s .co m this[i] = this[j]; this[j] = temp; } return this; };
// based on http://bost.ocks.org/mike/shuffle/ Array.prototype.shuffle = function() { var m = this.length, t, i; // While there remain elements to shuffle? while (m) {/*from w ww .j a v a 2 s .co m*/ // Pick a remaining element? i = Math.floor(Math.random() * m--); // And swap it with the current element. t = this[m]; this[m] = this[i]; this[i] = t; } return this; }
Array.prototype.shuffle = function () { for (var i = this.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = this[i]; this[i] = this[j];/*w w w . j a v a 2s. c om*/ this[j] = tmp; } return this; }
Array.prototype.shuffle = function() { var input = this; for (var i = input.length - 1; i >= 0; i--) { var randomIndex = Math.floor( Math.random() * ( i+1 ) ); var itemAtIndex = input[ randomIndex ]; input[ randomIndex ] = input[ i ]; /*from w w w . j a va 2 s .c o m*/ input[ i ] = itemAtIndex; } return input; }
Array.prototype.shuffle = function() { for ( var i = this.length-1; i > 0; i-- ) { var j = Math.floor( i * Math.random() ); var tmp = this[ j ]; this[ j ] = this[ i ];//from www.j a v a 2 s . co m this[ i ] = tmp; } return this; }
Array.prototype.shuffle = function() { var i = this.length, j, temp; while(--i>=0) { j=Math.floor(Math.random() * (i+1)); /* w ww. j a v a 2s . co m*/ temp = this[j]; this[j] = this[i]; this[i] = temp; } return this; }; var array = ['Monday','Tuesday','Wednesday','Friday','Saturday','Sunday']; var result = array.shuffle(); console.log(result);
Array.prototype.shuffle = function() { var len = this.length; var i = len;//from w w w . j av a2s .c om while (i--) { var p = parseInt(Math.random()*len); var t = this[i]; this[i] = this[p]; this[p] = t; } };
// Array extensions Array.prototype.shuffle = function() { for (i = this.length - 1; i > 0; i--) { var temp = this[i]; var random = Math.floor(Math.random() * (i + 1)); this[i] = this[random];/*w w w . j av a 2 s . c om*/ this[random] = temp; } return this; };
/** Shuffles this array. Returns self */ Array.prototype.shuffle = function () { var i, swap, ind; for (i = this.length - 1; i > 0; --i) { ind = parseInt(Math.random() * i); swap = this[i];//from w w w .j a v a 2 s .com this[i] = this[ind]; this[ind] = swap; } return this; };
Array.prototype.shuffle = function() { var i = this.length, j, temp; // j = random number while(--i > 0) { j = Math.floor(Math.random() * (i+1)); temp = this[j];/*w ww. j a v a2s . co m*/ this[j] = this[i]; this[i] = temp; } return this; } // fisher yates algorithm var arr = [1,2,3,4,5,6,7,8] var result = arr.shuffle(); console.log(result);
Array.prototype.shuffle = function () { var tmp, current, top = this.length; if (top) while(--top) { current = Math.floor(Math.random() * (top + 1)); tmp = this[current];/* w w w.j ava 2s . c o m*/ this[current] = this[top]; this[top] = tmp; } return this; };
Array.prototype.shuffle = function() { for (var i = this.length - 1; i > 0; i--) { var num = Math.floor(Math.random() * (i + 1)), d = this[num];/*from w w w. j a va 2 s. c om*/ this[num] = this[i]; this[i] = d; } return this; }
Array.prototype.shuffle = function() { for(var i=0; i<this.length; i++) this.swap(i, parseInt(Math.random() * this.length)); };
Array.prototype.shuffle = function() { var i = this.length, j, temp;//from w w w. java 2 s.c om if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[ i ]; this[ i ] = this[ j ]; this[ j ] = temp; } return this; } String.prototype.capitalize = function() { return this.charAt(0).toUpperCase() + this.slice(1); }
Array.prototype.shuffle = function() { var i = this.length if (i == 0) return this while (--i) { var j = Math.floor(Math.random() * (i + 1 )) var a = this[i] var b = this[j] this[i] = b/*from www. j a v a 2s . c o m*/ this[j] = a } return this }
Array.prototype.shuffle = function() { var swapbucket = this.length, randomelem, tempswap, tempelem; while (swapbucket--) { randomelem = Math.floor(Math.random() * (swapbucket + 1)); tempelem = this[randomelem];/*from w w w .ja v a 2s . com*/ tempswap = this[swapbucket]; this[randomelem] = tempswap; this[swapbucket] = tempelem; } } module.exports = Array;
Array.prototype.shuffle = function () { var i = this.length, j, temp; while (--i>0) { j=Math.floor(Math.random() * (i+1)); temp=this[j];//w ww . j a v a 2 s .c om array[j]=this[i]; array [i] = temp; } return this; }; var array = ['A','B','C','D','E','F','G','H']; var result = array.shuffle(); console.log(result);
const shuffle = a => a.shuffle(); Array.prototype.shuffle = function() { let m = this.length, i; while (m) {/*w w w . j a v a 2 s . c o m*/ i = ~~(Math.random() * m--); [this[m], this[i]] = [this[i], this[m]] } return this; }
Array.prototype.shuffle = function() { var i = this.length, j, temp; if ( i === 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];/*from ww w .jav a 2 s. com*/ this[i] = this[j]; this[j] = temp; } return this; };
//this method adds a shuffle routine to the javascript array Array.prototype.shuffle = function () { var input = this; var cnt = 1;/*w w w . j a va 2 s . c o m*/ for (var i = input.length - 1; i >= 0; i--) { var randomIndex = Math.floor(Math.random(cnt++) * (i + 1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i]; input[i] = itemAtIndex; } return input; }
// Knuth shuffle/*from ww w . ja va2s . c om*/ // - source: http://www.htmlblog.us/random-javascript-array Array.prototype.shuffle = 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; } }
// Extend array to do a shallow shuffle of the objects Array.prototype.shuffle = function() { for (i = this.length - 1; i > 0; i--) { var temp = this[i]; var random = Math.floor(Math.random() * (i + 1)); this[i] = this[random];//from ww w. j a v a 2s. co m this[random] = temp; } return this; };
'use strict';/*w w w .ja v a2s .c o m*/ Array.prototype.shuffle = function () { var i = this.length, j, temp; if (i == 0) return this; while (--i) { j = Math.floor(Math.random() * (i + 1)); temp = this[i]; this[i] = this[j]; this[j] = temp; } return this; }
// Fisher Yates Shuffle Algorithm Array.prototype.shuffle = function() { var i = this.length var j/* w w w . ja v a2s.c o m*/ var temp if (i > 1) { while (--i) { j = Math.floor(Math.random() * (i + 1)) temp = this[i] this[i] = this[j] this[j] = temp } } return this }
Array.prototype.shuffle = function shuffle() { var m = this.length, t, i; while ( m ) {// w w w .j a va2 s .co m i = Math.floor( Math.random() * m-- ); t = this[ m ]; this[ m ] = this[ i ]; this[ i ] = t; }; return this; };
/**//from w w w . j a v a 2 s .c o m * Implementation of Fisher-Yates shuffle in JS * 2014-01-21 */ Array.prototype.shuffle = function () { var end = this.length - 1; while (end > 1) { var r = Math.floor(Math.random() * end); this.push(this[r]); this.splice(r, 1); --end; } return this; }
// eslint-disable-next-line Array.prototype.shuffle = function() { let input = this; for (let i = input.length - 1; i >= 0; i--) { let randomIndex = Math.floor(Math.random() * (i + 1)); let itemAtIndex = input[randomIndex]; input[randomIndex] = input[i];/* w ww .j a va2 s . c o m*/ input[i] = itemAtIndex; } return input; };
Array.prototype.shuffle = function () { var newArr = []; for (var i = 0; i < this.length; i++) { var currElem = this[i]; var rand = Math.floor(Math.random() * this.length); this[i] = this[rand];//from w ww . j av a2 s .com this[rand] = currElem; } return this; }
Array.prototype.shuffle = function() { var len = this.length; var i = len;//from w ww .ja v a 2s . co m while (i--) { var p = parseInt(Math.random()*len); var t = this[i]; this[i] = this[p]; this[p] = t; } }; Array.prototype.filter_sort = function () { return this.sort(function(a,b){return a-b}); };
Array.prototype.shuffle = function() { return this.sort(function() { return Math.random() - 0.5; });//from ww w. jav a 2s . co m };
Array.prototype.shuffle = function() { var i = this.length, j, tempi, tempj; if ( i === 0 ) return false; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); tempi = this[i];/*from w w w .j av a 2 s. c om*/ tempj = this[j]; this[i] = tempj; this[j] = tempi; } return this; };
/** Given an array, guarantee * each element is shuffled once//from w w w . j a v a 2 s . c om * O(n) runtime */ Array.prototype.shuffle = function() { var remainingElements = this.length; var helper; var target; while (remainingElements) { target = Math.floor(Math.random()*remainingElements--); helper = this[remainingElements]; this[remainingElements] = this[target]; this[target] = helper; } }
Array.prototype.shuffle = function(){ var index1 = this.length, index2, temporary; while (--index1 > 0){ index2 = Math.floor(Math.random() * (index1 + 1)); temporary = this[index2];/*from w w w.ja v a 2 s.c o m*/ array[index2] = this[index1]; array[index1] = temporary; } return this; } var array = ['Jan','Feb','March','April','May','June','July','August']; var result = array.shuffle(); console.log(result);
Array.prototype.shuffle = function shuffle() { var currentIndex = this.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;//from w ww. jav a2 s. com // And swap it with the current element. temporaryValue = this[currentIndex]; this[currentIndex] = this[randomIndex]; this[randomIndex] = temporaryValue; } return this; }
Array.prototype.shuffle = function () { for (var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); }
Array.prototype.shuffle = function () { var currentIndex = this.length, temporaryValue, randomIndex; while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;/*from w w w . j a v a2s . c om*/ // And swap it with the current element. temporaryValue = this[currentIndex]; this[currentIndex] = this[randomIndex]; this[randomIndex] = temporaryValue; } };
/**/*from w w w. ja v a2 s . c o m*/ * Shuffles the items in an array. * Source: http://stackoverflow.com/a/10142256/710630 * * @returns {Array} */ Array.prototype.shuffle = function() { var i = this.length, j, temp; if (i === 0) { return this; } while (--i) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i]; this[i] = this[j]; this[j] = temp; } return this; };
function range(length) { var array = new Array(length); for (var i = 0; i < length; ++i) array[i]=i;/*w w w .j av a2 s . co m*/ return array; } function randomNext(top) { return Math.floor(Math.random() * top) } Array.prototype.shuffle = function() { for (var i = 0; i < this.length; ++i) { this.swap(i,randomNext(this.length)); } }; Array.prototype.swap = function(index1, index2) { var tmp = this[index1]; this[index1] = this[index2]; this[index2] = tmp; };
Array.prototype.shuffle = function() { var curindex = this.length, random_index; while (curindex) { random_index = (Math.random() * curindex--) | 0; [this[curindex], this[random_index]] = [this[random_index], this[curindex]] }// w w w .ja v a2 s . c o m return this; } function createTiles(n){ // TODO: Return array of bricks if(n<=0 || n%2 == 1){ return []; } var ret = []; for(i=1; i<=n/2; i++){ ret.push(i); ret.push(i); } return ret.shuffle(); }
Array.prototype.shuffle = function() { var currentIndex = this.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;//w ww . j av a2 s .c om // And swap it with the current element. temporaryValue = this[currentIndex]; this[currentIndex] = this[randomIndex]; this[randomIndex] = temporaryValue; } }
/**// www. j a v a 2 s.c o m * Fisher-Yates shuffle algorithm * @return {Array} shuffled array */ Array.prototype.shuffle = function() { var array = this.slice(); var current = this.length; var temp; var random; while(current !== 0) { //pick random element random = Math.floor(Math.random() * current); current -= 1; //swap random element with current element temp = array[current]; array[current] = array[random]; array[random] = temp; } return array; }
// Knuth shuffle// ww w. jav a 2 s .co m Array.prototype.shuffle = function() { for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this; };
Array.prototype.shuffle = function(){ var index1 = this.length, index2, temporary; while (--index1 > 0){ index2 = Math.floor(Math.random() * (index1 + 1)); temporary = this[index2];/*from w w w . jav a 2s .c om*/ array[index2] = this[index1]; array[index1] = temporary; } return this; } var array = ['A','B','C','D','E','F','G','H']; var result = array.shuffle(); console.log(result);
Array.prototype.shuffle = function() { var currentIndex = this.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;//from w w w. j a v a 2 s. co m // And swap it with the current element. temporaryValue = this[currentIndex]; this[currentIndex] = this[randomIndex]; this[randomIndex] = temporaryValue; } return this; }
Array.prototype.shuffle = function () { return this.sort(function () { return Math.random() - 0.5; });// w ww . ja v a 2 s . c o m }; [1,3,4,5].shuffle();
Array.prototype.shuffle = function() { var i = this.length; while (--i) { var j = Math.floor(Math.random() * (i + 1)) var temp = this[i]; this[i] = this[j];// w w w . j av a 2 s . c o m this[j] = temp; } return this; // for convenience, in case we want a reference to the array };
Array.prototype.shuffle = function() { var n=this.length,i1,i2; for(var i=0;i<n*2;i++) { i1=Math.floor(Math.random()*n); i2=Math.floor(Math.random()*n); var a=this[i1]; this[i1]=this[i2];/* w ww . j a va2 s . c o m*/ this[i2]=a; } return this; };
Array.prototype.shuffle=function(){//randomly shuffles an array and returns result for(i=this.length-1,tmp=this[i],target=0;i;tmp=this[--i]){ this[i]=this[(target=Math.floor(Math.random()*i))]; this[target]=tmp;// w w w. j av a2 s . com } return this; };
Array.prototype.shuffle=function(){ // Fisher-Yates//from w w w.j ava 2s.com var i=this.length; if (i==0) return; while (--i){ var rand=Math.floor(Math.random()*(i+1)); tempI=this[i]; tempRand=this[rand]; this[i]=tempRand; this[rand]=tempI; } }; arrayReturnRandom=function(arr,count){ arr.shuffle(); var randArr=[]; if (!count) count=1; for (i=0;i<count;i++){ randArr.push(arr[i]); } return randArr; };
Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i];/*from www. j a v a 2s . c o m*/ input[i] = itemAtIndex; } return input; }; var arr = [1,2,3,4]; console.log(arr.shuffle());
//the purpose of this algorithm is to iterate through an array and shuffle the contents unbiasly Array.prototype.shuffle = function() { var i = this.length, j, temp; while(--i > 0) { j = Math.floor(Math.random() * (i+1)); temp = this[i];//from w w w. j ava 2 s . co m this[i] = this[j]; this[j] = temp; } return this; } var arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',]; var result = arr.shuffle(); document.write(result);
/*!/*from w ww . j a v a2 s . c o m*/ * shuffle v0.0.1 * Released under the GPL 3.0 license * * Date: 2016-07-13 */ Array.prototype.shuffle = function (){ var temp, rand, round = Math.floor((Math.random() * (this.length*2)) + this.length); for (var i =0 ; round> i ; i++){ rand = Math.floor(Math.random() * this.length); temp = this[rand]; this.splice(rand, 1); this.push(temp); } return this; }
Array.prototype.shuffle = function () { for (var rnd, tmp, i = this.length; i; rnd = parseInt(Math.random() * i), tmp = this[--i], this[i] = this[rnd], this[rnd] = tmp); };
Array.prototype.shuffle = function() { let m = this.length, i; while (m) {//from w w w .ja va2 s. com i = ~~(Math.random() * m--); [this[m], this[i]] = [this[i], this[m]] } return this; }
Array.prototype.shuffle = function() { for(var j, x, k = this.length; k; j = Math.floor(Math.random() * k), x = this[--k], this[k] = this[j], this[j] = x); return this;/*ww w . j a v a 2 s. c om*/ };
Array.prototype.shuffle = function() { let i = 0,/*ww w . j a v a 2 s.c om*/ j = 0, temp = null; for (i = this.length - 1; i > 0; i--) { j = Math.floor(Math.random() * (i + 1)); temp = this[i]; this[i] = this[j]; this[j] = temp; } return this; }; module.exports = newDeck;
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;/*w w w . j a va 2 s. c o m*/ // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } Array.prototype.shuffle = function () { shuffle(this); };
Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i]; /*from w w w.jav a 2 s. co m*/ input[i] = itemAtIndex; } return input; };
Array.prototype.shuffle = function() { var currentIndex = this.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;/*from w ww.ja v a 2 s.co m*/ // And swap it with the current element. temporaryValue = this[currentIndex]; this[currentIndex] = this[randomIndex]; this[randomIndex] = temporaryValue; } return this; };
Array.prototype.shuffle = function() { var i = this.length, j, temp; if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];//from w w w. j av a 2 s .c om this[i] = this[j]; this[j] = temp; } return this; } function shuffle(array){ return Array.prototype.shuffle.call(array) }
Array.prototype.shuffle = function () { var self = this, n = self.length, i,/*from w ww.ja v a 2 s . c om*/ j, tmp; for (i = n - 1; i > 0; i -= 1) { j = Math.floor(Math.random() * (i + 1)); tmp = self[i]; self[i] = self[j]; self[j] = tmp; } return self; }; function Random(range) { range = range || 1; return (Math.round(Math.random() * range)); }
var card_table_class = "card"; Array.prototype.shuffle = function() { var x = this.length; while (x) {/* w w w .j a v a2s .co m*/ var y = Math.floor(Math.random() * x); var t = this[--x]; this[x] = this[y]; this[y] = t; } return this; } var random = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36); num = random.shuffle(); var r = -1; var card_td = new Array(); for (var i = 0; i <= 5; i++) { card_td[i] = new Array(); for (var j = 0; j <= 5; j++) { r++; card_td[i][j] = num[r]; } }
// much more simple, aha Array.prototype.shuffle = function() { for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this;/*from w ww .ja v a 2s . c om*/ }; // a little more complicated but work the same. Array.prototype.shuffleC = function function_name() { var m = this.length, t, i; while (m) { i = Math.floor(Math.random() * m--); t = this[m]; this[m] = this[i]; this[i] = t; } return this; }; var arr = [12, 290, 278, 21,300, 89, 78, 100, 29, 7, 206]; console.log(arr); console.log(arr.shuffle()); console.log(arr.shuffleC());
/*// ww w .j av a 2s . co m * @Shuffle * * add shuffle to Array prototype * */ Array.prototype.shuffle = function () { for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this; };
Array.prototype.shuffle = function() { var input = this; for (var i = input.length-1; i >=0; i--) { var randomIndex = Math.floor(Math.random()*(i+1)); var itemAtIndex = input[randomIndex]; input[randomIndex] = input[i];//from w w w . jav a 2 s. c o m input[i] = itemAtIndex; } return input; } Array.prototype.mode = function(){ if(this.length == 0) return null; var modeMap = {}; var maxEl = this[0], maxCount = 1; for(var i = 0; i < this.length; i++) { var el = this[i]; if(modeMap[el] == null) modeMap[el] = 1; else modeMap[el]++; if(modeMap[el] > maxCount) { maxEl = el; maxCount = modeMap[el]; } } return maxEl; } Array.prototype.multiMax = function(){ }
// Randomizes the element ordering and returns the Array Array.prototype.shuffle = 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); }//from w w w. ja v a2s. c o m var length = this.length; // Fisher?Yates implementation ("gold standard" of shuffles) for (var i = length - 1; i >= 0; i--) { var randomIndex = Math.floor(Math.random() * length), temp = this[i]; this[i] = this[randomIndex]; this[randomIndex] = temp; } return this; };
/*//from w w w .ja v a 2 s . c o m Write a function for doing an in-place ? shuffle of an array. The shuffle must be "uniform," meaning each item in the original array must have the same probability of ending up in each spot in the final array. Assume that you have a function get_random(floor, ceiling) for getting a random integer that is >=floor and <=ceiling. */ Array.prototype.shuffle = function(){ var length = this.length; var i, j, temp; for(i = length - 1; i >= 0; i--){ j = Math.floor(Math.random()*(i+1)); temp = this[i]; this[i] = this[j]; this[j] = temp; } return this; }; var testArray = [1,2,3,4,5,6,7,8,9,10]; console.log(testArray.shuffle()); console.log(testArray);
/**/*from w w w .ja v a2 s . co m*/ * Shuffle a given array * @param {array} array Array to be shuffled * @return {array} Suffled array */ Array.prototype.shuffle = function () { let array = this, arraylength = array.length, randomIndex, temp; while (arraylength) { randomIndex = Math.floor(Math.random(arraylength) * arraylength--); temp = array[arraylength]; array[arraylength] = array[randomIndex]; array[randomIndex] = temp; } return array; } // Test 1 console.log([].shuffle()); // Test 2 console.log([0].shuffle()); // Test 3 console.log([1].shuffle()); // Test 4 console.log([1,2].shuffle()); // Test 5 console.log([1,2,3].shuffle()); // Test 6 console.log([1,2,3,4,5,6,7,8,9,10,-1].shuffle());
// Thank you Michael Corrigan for this nice shuffle! http://blog.corrlabs.com/2011/02/shuffling-object-properties-in.html Array.prototype.shuffle = function(){ for (var i = 0; i < this.length; i++){ var a = this[i]; var b = Math.floor(Math.random() * this.length); this[i] = this[b];//from w w w . j a v a 2 s .c o m this[b] = a; } } function shuffleProperties(obj) { var new_obj = {}; var keys = getKeys(obj); keys.shuffle(); for (var key in keys){ if (key == "shuffle") continue; // skip our prototype method new_obj[keys[key]] = obj[keys[key]]; } return new_obj; } function getKeys(obj){ var arr = new Array(); for (var key in obj) arr.push(key); return arr; }
Array.prototype.shuffle = function() { var i = this.length, j, temp; if ( i == 0 ) return this; while ( --i ) { j = Math.floor( Math.random() * ( i + 1 ) ); temp = this[i];//from w ww.j av a 2 s. c o m this[i] = this[j]; this[j] = temp; } return this; } var pictures = new Array( "assets/boot.png", "assets/coffeecup.png", "assets/apple.png", "assets/truck.png", "assets/clock.png", "assets/fish.png", "assets/duck.png", "assets/house.png" ); function shufflePictures() { pictures.shuffle(); for (i = 0; i < pictures.length; i++) { imgSrc = pictures[i]; imgAlt = imgSrc.replace(/^.*\//, ''); imgTag = document.getElementById('pic' + (i+1)); imgTag.src = imgSrc; imgTag.alt = imgAlt; } }
Array.prototype.shuffle = function() { var m = this.length, t, i; while (m) {// w ww. ja v a 2 s . co m i = Math.floor(Math.random() * m--); t = this[m]; this[m] = this[i]; this[i] = t; }; return this; };
Array.prototype.shuffle = function () { var l = this.length + 1; while (l--) {//from ww w . ja v a 2s.c o m var r = ~~(Math.random() * l), o = this[r]; this[r] = this[0]; this[0] = o; } return this; };
Array.prototype.shuffle = function () { var i = this.length, j, temp; if (i == 0) return this; while (--i) { j = Math.floor(Math.random() * ( i + 1 )); temp = this[i];/*from www . j a v a2 s . c om*/ this[i] = this[j]; this[j] = temp; } return this; };
// http://javascript.about.com/library/blshuffle.htm Array.prototype.shuffle = function() { var s = [];//from w w w . j a v a 2 s .co m while (this.length) s.push(this.splice(Math.random() * this.length, 1)); while (s.length) this.push(s.pop()); return this; };
// Fisher-Yates shuffle on an array. Array.prototype.shuffle = function() { var counter = this.length; while (counter) {/*w ww.j av a2s . c o m*/ var index = random(counter); --counter; var temp = this[counter]; this[counter] = this[index]; this[index] = temp; } return this; }
/// Knuth shuffling Array.prototype.shuffle = function() { for (var i = 1; i < this.length; ++i) { this.swap(i, rand(0, i));//from www .j av a 2 s. c om } }