求在javascript中生成十个一到五十之间的随机数字,且随机数字不重复,且随机数要冒泡
2个回答
展开全部
<script>
function getNum(){
var nums = new Array()
var flgs = new Array();
for(var i=0;i<=50;i++){
flgs.push(false);
}
while(nums.length<10){
var r = Math.random();
var num = r*50 + 1;
num = parseInt(num);
if(flgs[num]){
continue;
}
nums.push(num);
flgs[num]=true;
}
nums.sort(compareInt);
alert(nums);
return nums;
}
function compareInt(int1, int2){
var iNum1 = parseInt(int1);
var iNum2 = parseInt(int2);
if(iNum1 < iNum2){
return -1;
}else if(iNum1 > iNum2){
return 1;
}else{
return 0;
}
}
</script>
展开全部
var L = {
orial: [],
result: [],
rand: function(){
for (var i = 0; i < 10; i++) {
L.result.push(Math.floor(1 + Math.random() * 50));
}
L.orial = L.result.slice (0);
},
sorter: function(){
L.result.sort(function(first, second){
if (first > second) {
return -1;
} else if (first < second) {
return 1;
} else {
return 0;
}
});
}
};
L.rand();
console.log(L.orial);
L.sorter();
console.info(L.result);
orial: [],
result: [],
rand: function(){
for (var i = 0; i < 10; i++) {
L.result.push(Math.floor(1 + Math.random() * 50));
}
L.orial = L.result.slice (0);
},
sorter: function(){
L.result.sort(function(first, second){
if (first > second) {
return -1;
} else if (first < second) {
return 1;
} else {
return 0;
}
});
}
};
L.rand();
console.log(L.orial);
L.sorter();
console.info(L.result);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询