JavaScript隨機打亂數(shù)組順序之隨機洗牌算法
假如有一個數(shù)組是這樣子:
var arr1 = ["a", "b", "c", "d"];
如何隨機打亂數(shù)組順序,也即洗牌。
有一個比較廣為傳播的簡單隨機算法:
function RandomSort (a,b){ return (0.5 - Math.random()); }
實際證明上面這個并不完全隨機。
隨便一搜網(wǎng)上太多這種東西了,看一下stackoverflow上的一個高分回答,答案出自github上。
knuth-shuffle
The Fisher-Yates (aka Knuth) shuffle for Browser and Node.JS
下面一起看看上面說的這個算法,代碼如下:
/*jshint -W054 */
(function (exports) {
'use strict';
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
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;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
exports.knuthShuffle = shuffle;
}('undefined' !== typeof exports && exports || 'undefined' !== typeof window && window || global));
作者推薦使用瀏覽器寫法:
(function () {
'use strict';
var a = [2,11,37,42]
, b
;
// The shuffle modifies the original array
// calling a.slice(0) creates a copy, which is assigned to b
b = window.knuthShuffle(a.slice(0));
console.log(b);
}());
Nodejs:
npm install -S knuth-shuffle
(function () {
'use strict';
var shuffle = require('knuth-shuffle').knuthShuffle
, a = [2,11,37,42]
, b
;
// The shuffle modifies the original array
// calling a.slice(0) creates a copy, which is assigned to b
b = shuffle(a.slice(0));
console.log(b);
}());
還有其它從這個算法中變形去的,比如下面這個for循環(huán)的。其它的就不說了。
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
使用ES2015(ES6)
Array.prototype.shuffle = function() {
let m = this.length, i;
while (m) {
i = (Math.random() * m--) >>> 0;
[this[m], this[i]] = [this[i], this[m]]
}
return this;
}
使用:
[1, 2, 3, 4, 5, 6, 7].shuffle();
發(fā)現(xiàn)中文搜索隨機算法一大堆,但究竟是不是完全隨機,效率和兼容性都有待考究,建議后面如果有需要用到隨機打亂數(shù)組元素,可以用上面這個。
相關(guān)文章
JavaScript 監(jiān)聽組合按鍵思路及代碼實現(xiàn)
這篇文章主要介紹了JavaScript 監(jiān)聽組合按鍵思路及代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JS中showModalDialog關(guān)閉子窗口刷新主窗口用法詳解
這篇文章主要介紹了JS中showModalDialog關(guān)閉子窗口刷新主窗口用法,結(jié)合具體實例形式較為詳細的分析了showModalDialog常見用法與相關(guān)使用技巧,需要的朋友可以參考下2017-03-03
詳解Bootstrap的aria-label和aria-labelledby應(yīng)用
這篇文章主要介紹了詳解Bootstrap的aria-label和aria-labelledby應(yīng)用的相關(guān)資料,需要的朋友可以參考下2016-01-01
Javascript在IE和Firefox瀏覽器常見兼容性問題總結(jié)
這篇文章主要介紹了Javascript在IE和Firefox瀏覽器常見兼容性問題,結(jié)合實例形式總結(jié)分析了javascript在IE與Firefox瀏覽器中常見的各種兼容性問題與相應(yīng)的解決方法,需要的朋友可以參考下2016-08-08
javascript的解析執(zhí)行順序在各個瀏覽器中的不同
javascript是一種解釋型語言,它的執(zhí)行是自上而下的。由于各個瀏覽器對它的理解有所差異,所以我們有必要深入理解js的執(zhí)行順序2014-03-03
關(guān)于javascript中的promise的用法和注意事項(推薦)
這篇文章主要介紹了關(guān)于javascript中的promise的用法和注意事項,需要的朋友可以參考下2021-01-01

