Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

写一个乱序函数 #35

Open
Saikikoko opened this issue Mar 17, 2021 · 0 comments
Open

写一个乱序函数 #35

Saikikoko opened this issue Mar 17, 2021 · 0 comments
Labels
Js knowledge of Javascript

Comments

@Saikikoko
Copy link
Owner

function shufftle(a) {
  for (let i = 1; i < a.length; i++) {
    let j = Math.floor(Math.random() * i);
    [a[i], a[j]] = [a[j], a[i]]
  }
  return a
}

var times = 100000;
var res = {};

for (var i = 0; i < times; i++) {
    var arr = shuffle([1, 2, 3]);

    var key = JSON.stringify(arr);
    res[key] ? res[key]++ :  res[key] = 1;
}

// 为了方便展示,转换成百分比
for (var key in res) {
    res[key] = res[key] / times * 100 + '%'
}

console.log(res)
@Saikikoko Saikikoko added the Js knowledge of Javascript label Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Js knowledge of Javascript
Projects
None yet
Development

No branches or pull requests

1 participant