Using the following array as an example:
Array = [“Item1”,“Item2”,“Item3”,“etc”];
I am trying to figure out how to pull items from an array randomly. How do I setup the AS so when you click a button it will select a randon item from the array and display that item in dynamic text box? Sorry dont know AS that well but trying to learn.
Well, it depends if you want to pull the elements of the array one by one until the array is empty (a bit of array manipulation there), or if you want to take 1 randomly like Lost did (you should explain that code a bit, by the way :)).
I just want to take 1 randomly. i tried the code lost posted but didnt seem to work. I will try playing with it some more tomorrow but not knowing AS that well its hard to troubleshoot te code…Thanks for the help guys!
Thanks Lost! I was putting the ‘setArray’ as an instance name instead of a var and thats why it was not working. Havnet had a chance to study the code yet but im sure I might have some questions…Thanks agian!
*Originally posted by ilyaslamasse *
**Someone has to write an article about arrays, there are so many questions about them :-\ Anyone feels like doing this? [SIZE=1]lost? :beam:[/SIZE] **
That is a nice article. My problem with arrays is not understanding the array itselfs but how to manipulate them to do what you want…That article goes pretty in depth into that however.
On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on actionscript.org and didnt see anything…they do have some nice tuturiols though.
*Originally posted by byis *
**On a second note, is there a site that has a list of the different cmds in AS and a desription for each? I checked on actionscript.org and didnt see anything…they do have some nice tuturiols though. **
If you use MX you could take a look at the AS reference. (its the little book by the expert/normal mode button)
if you scroll down there is a “yellow” book called index, open that up, and it is an alphabetizied list. its a pretty dry read, but there are a ton there, good info too, like which versions support a certain command and whatnot.
can someone explain this statement. I kinda have a “grasp” on it, but
i don’t want to go on, if I don’t have it right.
array.length is how many elements in the array. (lets use 5 for my example.)
math.random() returns a random number greater than or equal to zero, and less than 1.
and floor will take the number from this expression and change it to the number that is equal to or rounded down.
so if math.random gave us .3345 and length was 5. that ==1.6725
then floor turns that into 1
and then that would return the 2nd element in the array.
right?
ok, so if that is right, wouldn’t it seem that the last element in the array would
be chosen the least amount of times???
Math.floor rounds down as you said, that is the only way to get to 0, which is where an array starts counting from.
So Math.floor has the chance to round down to 0, and multiplying it by the length of the array makes sure that it has a chance of hitting that last one as well without going past it and getting a blank (null) value.
thanks for your help so far. one last thing.
getting back to what I said earlier wouldn’t it seem that logicallly if flash was pulling (math)random numbers and then getting the floor of those numbers, that randomly generating the last element would happen the least amount of times?