# 编程基础：随机且不重复的算法

var containerSet = function(count) {
var tempArr = [];

for (var i=0; i < count; i++){
tempArr[i]=i;
}
tempArr.sort(function() {
return 0.5 - Math.random();
});
return tempArr;
}

var containers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'];
var l = containers.length;
var countSet = containerSet(l);
var resultSet = [];
while(l--) {
resultSet.push(containers[countSet[l]]);
}

console.log(resultSet);


tempArr.sort(function() {
return 0.5 - Math.random();
});


If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

• If compareFunction(a, b) is less than 0, sort a to a lower index than b.
• If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
• If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
• compareFunction(a, b) must always returns the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined
posted on 2010-09-17 15:36  豆豆の爸爸  阅读(495)  评论(0编辑  收藏  举报