假如有一个数组是这样子:
var arr1 = ["a", "b", "c", "d"];
如何随机打乱数组顺序,也即洗牌。
有一个比较广为传播的简单随机算法:
function RandomSort (a,b){ return (0.5 - Math.random()); }
实际证明上面这个并不完全随机。
随便一搜网上太多这种东西了,看一下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循环的。其它的就不说了。
/** * 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();
发现中文搜索随机算法一大堆,但究竟是不是完全随机,效率和兼容性都有待考究,建议后面如果有需要用到随机打乱数组元素,可以用上面这个。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】