You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
476 B
25 lines
476 B
5 months ago
|
var random = require('./random')
|
||
|
|
||
|
var values = require('./values')
|
||
|
|
||
|
/**
|
||
|
* 将一个数组随机打乱,返回一个新的数组
|
||
|
*
|
||
|
* @param {Array} array 数组
|
||
|
* @return {Array}
|
||
|
*/
|
||
|
function shuffle (array) {
|
||
|
var index
|
||
|
var result = []
|
||
|
var list = values(array)
|
||
|
var len = list.length - 1
|
||
|
for (; len >= 0; len--) {
|
||
|
index = len > 0 ? random(0, len) : 0
|
||
|
result.push(list[index])
|
||
|
list.splice(index, 1)
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
module.exports = shuffle
|