验证中...
Languages: JavaScript
Categories: 算法分析
Latest update 2019-09-11 17:43
gistfile1.txt
Raw Copy
function shuffle(arr) {
//将原数组改为对象数组(值、随机编号 为对象的两个属性)
let new_arr = arr.map(i => ({v: i, r: Math.random()}));
//将对象数组 按照随机编号进行排序
new_arr.sort((a, b) => a.r - b.r);
//将数组提取出v值,插入到原数组中
arr.splice(0, arr.length, ...new_arr.map(i => i.v));
}
let a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let n = 10000;
let count = (new Array(a.length)).fill(0);
for (let i = 0; i < n; i ++) {
shuffle(a);
count[a.indexOf('a')]++;
}
console.log(a)

Comment list( 0 )

You need to Sign in for post a comment

Help Search