Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random

Status
Not open for further replies.

jocoutts

Technical User
Feb 5, 2002
32
GB
I know how to make ActionScript choose a random number between 1 and ?.
But how exactly can ActionScript choose a sequence of numbers between, say 1 and 20, in random order, but so they are not repeated.
At the moment, I have some code which instructs the frame to pick a random number between 1 and 20. I then have a long, lengthy and probably needless script that goes over each number, if it has already been previously picked then it is ignored.
As this script is in a frame, this is a lengthy process. When there is only one number left to pick, it can take a long time before the computer finally picks this one.
Can anyone help?
 
Try this - attach it to any clip then trigger the random number generation with a mouse click. It should be easy enough to convert this code to sit on a frame but it's easier to put the point across with it all in one place.

onClipEvent (load) {
// do the actual array manipulation
function pickRandom () {
randomNumber = Math.floor(Math.random()*availableNumbers.length);
newValue = availableNumbers.splice(randomNumber, 1);
return newValue;
}
// list of all available values
availableNumbers = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20");
}
onClipEvent (mouseDown) {
// generate list on mouse click
for (i=0; i<20; i++) {
list += pickRandom()+&quot;, &quot;;
trace (list);
}
}
Slainte

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top