Skip to content

Instantly share code, notes, and snippets.

@linkgod
Last active August 29, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkgod/9f08e4764eef85c04daa to your computer and use it in GitHub Desktop.
Save linkgod/9f08e4764eef85c04daa to your computer and use it in GitHub Desktop.
随机截取一个列表中的元素,返回列表
/*
* @description 获取一个list孩子随机生成器
* @param {Array} list 列表
* @param {Array} shown 已经显示过的项目
* @return {Function} list随机孩子的列表生成器,执行之后返回一个随机孩子列表
* */
function randomChildGenerator(list) {
var listProduct = list,
listCustom = [];
if (!S.isArray(list)) {
S.error('first parma should be array');
return ;
}
return function(amount){
var r = [],
temp,
item;
amount = amount ? amount : 1;
if (listProduct.length < amount) {
r = r.concat(listProduct);
temp = listProduct;
listProduct = listCustom;
listCustom = temp;
}
for (var len = listProduct.length; r.length < amount; len--) {
item = listProduct.splice(util.randomInt(len - 1), 1)[0];
r.push(item);
listCustom.push(item);
}
return r;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment