HI,
Today I have to generate Non Repetitive random number. means a number may me 1234 but not 1224 means no same digit repeat on same number.
javascritp function is as follows :_
function generateRandomNumber(n)
{
if( n > 10)
{
alert('Please Enter 2 to 10');
return false;
}
var randomNo;
var finalArray=new Array();
while(finalArray.length < n)
{
randomNo = Math.floor(Math.random()*10);
if(finalArray.join('').indexOf(randomNo,0) == -1)
{
finalArray[finalArray.length] = randomNo;
}
}
document.getElementById("lbl").innerHTML = finalArray.join('');
return false;
}
Where lbl is id of label where we have to show the random number.
Thanks
Help OnDesk Team
No comments:
Post a Comment