Need an answer quick: Random()

This should be a really easy question. Can random() select randomly between letters or strings instead of numbers?
Like this maybe:

milk = random(cookie,box,spoon);

Is something like this possible?

No, I don’t think it does. But please someone correct me if I’m wrong.

You would however be able to generate a random number, then use that to pull a string out of an array, for example.

yeah… I would do that with a simple array situation… that’s what they are for

storage=New array(cookie,box,spoon);


or something like that anyway

Can you please explain this line?



‘something’ is a variable that I am setting to equal one of the array items. It could be anything including a dynamicaly created variable… though that seems redundent with an array already set up.

The line “storage[Math.floor(Math.random()*3+1)];” is supposed to do this… but I haven’t tested it… it’s just from my head

storage[ this is the array name as set up in the first line. What belongs inside it’s square brackets is a number. (I already see a mistake… I’ll fix it below) But we can be tricky and place a variable or an expression in the square brackets instead, as long as it translates to a valid number.

Math.floor(Math.random()*3+1) this is supposed to be generating a number between 1 and 3, but I forgot that an array’s first item is number 0, not number 1. It’s a mistake I make all the time. So the a/s should really be


Math.floor(); rounds whatever is inside it’s brackets to the next lowest whole number.
Math.random() produces a number between 0.00 and 1.00. Multiply that by three and you get a number between 0.00 and 2.00 (3*.99=2.97) rounded down at least… it will always be 0,1,or 2.

] then finaly a closing square bracket

These are the fun commands where you start getting wild ideas about dynamic creation and sorting of information. Be prepared… you will soon be insane like the rest of us. :wink:

I don’t get it why can’t you just do this: Math.random(2) ?

Math.random(3) = Messy decimal numbers

Math.floor(Math.random()*3) = Nice whole numbers that are rounded down (rounded down so you can get zero)

Math.random(3); would be improper syntax. The old style was
random(3); but Math.random(3) wont do what you’re thinkin.

Haha, good point, it does work though david. That is what I have been meaning to ask you but forgot.

I know it is written as Math.random()*3 (and that is how I always write it), but why is Math.random(3) considered improper syntax when it works perfectly fine?

hmm… I don’t know. I’ve just always believed that it wouldn’t work. Perhaphs I just haven’t experiemented with it.

Ah yes, after re-experimenting with it while using the trace function, I found it only returns 1’s and 0’s if you have it has Math.random(3)… makes me wonder what I did different when I first tested it…eh oh well, I still know I do it right now.

using Math.random(3); I came up with these number out of ten tries


using Math.random()*3; I came up with these


So the Math.random(3); is not working. Not the way random is supposed to work anyway.

you probebly did


the first time. That is depricated, but it still works, and will do a random 1 through 3

I just used Math.round when I did it because all of those decimals get on my nerves…:slight_smile: I got…


You get the point.

I Probably did. It was back when I tried AS for the very first time.

Thanks for all the help guys! (and gals :wink: )

Just a little something:

storage=New array("cookie","box","spoon");

would be better if the length of storage changes.

pom :asian:

Smart Pom… always thinking.

It’s Pom, who would expect less:)


And Lost, I’ve just noticed you have more posts than Upu himself! Now correct me if I’m wrong, Upu, but I don’t think this has ever happened to you, has it? Even on the old forums?

pom :stuck_out_tongue: