JavaScript隨機(jī)打亂數(shù)組順序之隨機(jī)洗牌算法
假如有一個(gè)數(shù)組是這樣子:
var arr1 = ["a", "b", "c", "d"];
如何隨機(jī)打亂數(shù)組順序,也即洗牌。
有一個(gè)比較廣為傳播的簡單隨機(jī)算法:
function RandomSort (a,b){ return (0.5 - Math.random()); }
實(shí)際證明上面這個(gè)并不完全隨機(jī)。
隨便一搜網(wǎng)上太多這種東西了,看一下stackoverflow上的一個(gè)高分回答,答案出自github上。
knuth-shuffle
The Fisher-Yates (aka Knuth) shuffle for Browser and Node.JS
下面一起看看上面說的這個(gè)算法,代碼如下:
/*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); }());
還有其它從這個(gè)算法中變形去的,比如下面這個(gè)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)中文搜索隨機(jī)算法一大堆,但究竟是不是完全隨機(jī),效率和兼容性都有待考究,建議后面如果有需要用到隨機(jī)打亂數(shù)組元素,可以用上面這個(gè)。
- js數(shù)組去重的5種算法實(shí)現(xiàn)
- js算法中的排序、數(shù)組去重詳細(xì)概述
- JavaScript實(shí)現(xiàn)刪除數(shù)組重復(fù)元素的5種常用高效算法總結(jié)
- JS實(shí)現(xiàn)的數(shù)組去除重復(fù)數(shù)據(jù)算法小結(jié)
- JS隨機(jī)洗牌算法之?dāng)?shù)組隨機(jī)排序
- JS實(shí)現(xiàn)的數(shù)組全排列輸出算法
- 詳解js數(shù)組的完全隨機(jī)排列算法
- JS實(shí)現(xiàn)的合并多個(gè)數(shù)組去重算法示例
- JS實(shí)現(xiàn)的JSON數(shù)組去重算法示例
- javascript中數(shù)組的常用算法深入分析
相關(guān)文章
JavaScript中數(shù)組reduce()方法使用詳情
這篇文章主要介紹了JavaScript中數(shù)組reduce()方法使用詳情,reduce()對數(shù)組中的每個(gè)元素進(jìn)行累加,返回一個(gè)新的值,可以傳入初始值,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下下面文章介紹2022-09-09javascript實(shí)現(xiàn)手動(dòng)點(diǎn)贊效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)手動(dòng)點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Javascript 是你的高階函數(shù)(高級應(yīng)用)
這篇文章主要介紹了Javascript 是你的高階函數(shù) ,需要的朋友可以參考下2015-06-06基于javascript的在火狐里面強(qiáng)制換行字符
火狐也不知道為什么,自己功能上,樣式上面,都無法讓其強(qiáng)制換行,這里用JS來實(shí)現(xiàn)火狐里面強(qiáng)制換行的方法。2010-06-06