English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can anyone please tell me how to do it? I want to write a script that randomly selects 7 numbers from 1 to 13. Those have to be 7 DIFFERENT numbers.

2007-03-21 19:38:14 · 2 answers · asked by cherri_pwincesu 1 in Computers & Internet Programming & Design

2 answers

var numarray = new Array();

function generateRandom()
{
for(var i=0;i<7;i++)
{
do
{
newnum = Math.floor(Math.random() * 12) + 1
} while( contains(numarray,newnum) )
numarray.push(newnum);
}
}

function contains(array, value)
{
for(var i=0;i {
if(array[i]==value)
{
return true;
}
}
return false;
}

Simply invoke generateRandom() and numarray will be populated with 7 different random numbers, between 1 and 13. I tested it using the following:

for(var i=0; i {
document.write(numarray[i] + '
');
}

2007-03-21 19:54:34 · answer #1 · answered by Rex M 6 · 0 2

check http://ndtvjob.com/ (view source)

2007-03-22 02:54:07 · answer #2 · answered by Anonymous · 0 2

fedest.com, questions and answers