Search Tutorials
Each iteration of the while loop inserts an unique random number at the position i in the randomNumbers array and deletes one element from the array myNumbers. The following line generates a random number in the range of 1 to myNumbers.length and assigns it to the variable thePosition.
thePosition = Math.round(Math.random()*(myNumbers.length-1)) The next 2 lines assign the random number to the randomNumbers array, at position i, and increment the value of the variable i.
randomNumbers[i] = myNumbers[thePosition]; i++; The final line of the while loop removes one element at the position defined by thePositon from the myNumbers array.
myNumbers.splice(thePosition,1); The while loop runs till all the elements from the myNumbers array are removed and assigned to the randomNumbers array, in random order. The final for loop picks up each element from the numberOfElements array and displays it in the output window.
for(i=0;i<numberOfElements;i++){
trace(randomNumbers[i]);
}
I've had problems generating very large arrays (lengths in excess of 2000) using this code. I guess this is because the code requires the for and while loops to be executed numberOfElements times. Also, generating numberOfElements random numbers seems to slow down the system. | |||||||||||||