Solved

Avoiding the repetion of random numbers. HOW DO I PLACE MY CODE IN LOOP OF A FUNCTION

Posted on 2004-11-10
137 Views
stiil struggling:
words=["Which is the capitol of Italy?","Which is the capitol of Spain?","Which is the capitol of France?","Which is the capitol of Holland?"];
randvalue=Math.round(Math.random()*4);
word=words[randvalue];
trace(word);

This was my help to the first question:

randValue = Math.round(Math.random()*words.length);
word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
trace word;

HOW DO I  place thhis in a function of a loop, that should do it. (THIS IS MY QUESTION!!!!!)
0
Question by:andreafp
1 Comment

LVL 10

Accepted Solution

Sorry about the wording, should have been "place in a function OR a loop", so for a loop:

words=["Which is the capitol of Italy?","Which is the capitol of Spain?","Which is the capitol of France?","Which is the capitol of Holland?"];
for(var i = 0; i < words.length; i++){
randValue = Math.round(Math.random()*words.length);
word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
//do what you need to do here, assign to an array to store them randomly, or som other action
}

that will loop through, now keep in mind that that will loop through each of the items in a random order, but won't stop (as it's written) to do anything else.

or for a function to be called, place this on the first frame:
function getWord():String{
randValue = Math.round(Math.random()*words.length);
word = words.splice[randvalue, 1];  //removes that value from the array ensuring it's not repeated, and assigns it to word
return word;
}

then if you call that function:
myNewWord = getWord();
It will get you a new word from your list.  This particular setup has no checking for whether you're at the end of the list though.  Feel free to respond here if any of that doesn't make sense.
0

Featured Post

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.