CV肉饼王

Talk is cheap. Show me the code.

0%

【JS】数组取随机数

一、使用数组sort方法对数组元素随机排序,并取指定长度的元素

1
2
3
4
5
6
7
Array.prototype.shuffle = function (n) {
var len = this.length,
num = n ? Math.min(n, len) : len,
arr = this.slice(0);
arr.sort(function (a, b) { return Math.random() - 0.5; });
return arr.slice(0, num - 1);
};

二、随机交换数组内的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
lib = {};
lib.range = function (min, max) { return min + Math.floor(Math.random() * (max - min + 1)); };

Array.prototype.shuffle = function (n) {
var len = this.length,
num = n ? Math.min(n, len) : len,
arr = this.slice(0),
temp,
index;
for (var i = 0; i < len; i++) {
index = lib.range(i, len - 1);
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
return arr.slice(0, num);
};

三、随机从原数组抽取一个元素,加入到新数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
lib = {};
lib.range = function (min, max) { return min + Math.floor(Math.random() * (max - min + 1)); };
Array.prototype.shuffle = function (n) {
var len = this.length,
num = n ? Math.min(n, len) : len,
arr = this.slice(0),
result = [],
index;
for (var i = 0; i < num; i++) {
index = lib.range(0, len - 1 - i);
// 或者 result.concat(arr.splice(index,1))
result.push(arr.splice(index, 1)[0]);
}
return result;
};

简易版本

1
2
3
"use strict";
let arr = [1, 2, 3, 4, 5, 6];
console.log(arr.sort(() => Math.random() - 0.5)[0]);

参考:JavaScript学习笔记:数组随机排序