-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path洗牌算法 Shuffle.html
83 lines (74 loc) · 2.11 KB
/
洗牌算法 Shuffle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Shuffle</title>
</head>
<style>
body {
padding: 16px 32px;
}
div {
line-height: 2
}
</style>
<body>
<div>
<a href="https://www.h5jun.com/post/array-shuffle.html">数组的完全随机排列</a>
</div>
<div>
<a href="https://github.com/hanzichi/underscore-analysis/issues/15">JavaScript
数组乱序</a>
</div>
<div>
<a href="https://www.kirupa.com/html5/shuffling_array_js.htm">Shuffling an Array in JavaScript</a>
</div>
</body>
<script>
function append(value) {
const el = document.createElement('div')
el.innerHTML = value
document.body.appendChild(el)
}
</script>
<script>
// 方法一
function shuffle(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
var idx = Math.floor(Math.random() * (len - i));
var temp = arr[idx];
arr[idx] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
return arr;
}
// 方法二
function shuffle(arr) {
let len = arr.length, random;
while (len) {
random = (Math.random() * len--) >>> 0; // 无符号右移位运算符向下取整
[arr[len], arr[random]] = [arr[random], arr[len]] // ES6 的结构赋值实现变量互换
}
return arr;
}
var arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48, 3, 44, 38, 5, 47, 15, 36];
console.log(shuffle(arr))
append(JSON.stringify(arr))
// var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// var res = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// var t = 10000;
// for (var i = 0; i < t; i++) {
// var sorted = shuffle(arr.slice());
// sorted.forEach(function (v, i) {
// res[i] += v;
// });
// }
// res = res.map(function (v) {
// return v / t;
// });
// console.log(res);
</script>
</html>