生成指定个数的随机数,有范围
<script language="JavaScript" type="text/javascript">
function randomGen(low,high, count){
var myArray = new Array();
if( high - low + 1 < count){
alert("error with params!");
return;
}
for( var i = 0; i < count; i++ ){
var oneRand = randInt(low,high);
var isContain = false;
for( var j = 0; j < myArray.length; j++ ){
if( myArray[j] == oneRand ){
isContain = true;
break;
}
}
if( !isContain )
myArray.push(oneRand);
else
i--;
}
return myArray;
}
function randInt(low,high) {
return Math.floor(Math.random()*(high-low+1)+low);
}
// here is the test
var testArray = randomGen( 3, 90, 80 );
for( var k = 0 ; k < testArray.length; k ++){
document.write(testArray[k] + "<br />");
}
</script>
function randomGen(low,high, count){
var myArray = new Array();
if( high - low + 1 < count){
alert("error with params!");
return;
}
for( var i = 0; i < count; i++ ){
var oneRand = randInt(low,high);
var isContain = false;
for( var j = 0; j < myArray.length; j++ ){
if( myArray[j] == oneRand ){
isContain = true;
break;
}
}
if( !isContain )
myArray.push(oneRand);
else
i--;
}
return myArray;
}
function randInt(low,high) {
return Math.floor(Math.random()*(high-low+1)+low);
}
// here is the test
var testArray = randomGen( 3, 90, 80 );
for( var k = 0 ; k < testArray.length; k ++){
document.write(testArray[k] + "<br />");
}
</script>