-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
39 lines (34 loc) · 737 Bytes
/
index.html
File metadata and controls
39 lines (34 loc) · 737 Bytes
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>随机打乱一个一维数组</title>
</head>
<body>
<script type="text/javascript">
// 方法一
var arr = [];
for (var i = 0; i < 10; i++) {
arr[i] = i;
}
arr.sort(function() {
return 0.5 - Math.random()
})
var str = arr.join();
document.write(str + "<br>");
//方法二
function shuffle(arr) {
var len = arr.length;
for (var i = 0; i < len; i++) {
var j = Math.floor(Math.random() * len);
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
shuffle(arr);
str = arr.join();
document.write(str + "<br>");
</script>
</body>
</html>