View Full Version : need an answer quick: Random()
__FLATLINE__
September 10th, 2002, 07:12 AM
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?
Kitiara
September 10th, 2002, 07:49 AM
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.
upuaut
September 10th, 2002, 09:11 AM
yeah.. I would do that with a simple array situation.. that's what they are for
storage=New array(cookie,box,spoon);
something=storage[Math.floor(Math.random()*3+1)];
or something like that anyway
__FLATLINE__
September 10th, 2002, 09:15 AM
Can you please explain this line?
something=storage[Math.floor(Math.random()*3+1)];
Thanks.
upuaut
September 10th, 2002, 09:25 AM
'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
storage[Math.floor(Math.random()*3)];
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. ;)
__FLATLINE__
September 10th, 2002, 11:46 AM
I don't get it why can't you just do this: Math.random(2) ?
lostinbeta
September 10th, 2002, 01:14 PM
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)
upuaut
September 10th, 2002, 11:44 PM
Math.random(3); would be improper syntax. The old style was
random(3); but Math.random(3) wont do what you're thinkin.
lostinbeta
September 10th, 2002, 11:46 PM
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?
upuaut
September 10th, 2002, 11:49 PM
hmm.. I don't know. I've just always believed that it wouldn't work. Perhaphs I just haven't experiemented with it.
lostinbeta
September 10th, 2002, 11:52 PM
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.
upuaut
September 11th, 2002, 12:01 AM
using Math.random(3); I came up with these number out of ten tries
0.182616225528818
0.246697391498227
0.496306900166118
0.294558602056726
0.0384465507410683
0.804301160762227
0.274645778944085
0.671068015820844
0.376926560130402
0.70065694567778
using Math.random()*3; I came up with these
1.44232590051476
2.42299682899518
0.169483414930051
0.996716877909711
1.47894309949081
1.74336946464301
1.63587004488142
2.7064569735464
0.101575112948928
1.76893623628138
So the Math.random(3); is not working. Not the way random is supposed to work anyway.
upuaut
September 11th, 2002, 12:02 AM
you probebly did
random(3);
the first time. That is depricated, but it still works, and will do a random 1 through 3
lostinbeta
September 11th, 2002, 12:03 AM
I just used Math.round when I did it because all of those decimals get on my nerves....:) I got....
1
1
1
0
0
1
0
1
You get the point.
lostinbeta
September 11th, 2002, 12:04 AM
I Probably did. It was back when I tried AS for the very first time.
__FLATLINE__
September 11th, 2002, 03:34 AM
Thanks for all the help guys! (and gals ;) )
pom
September 11th, 2002, 03:35 AM
Just a little something:
storage=New array("cookie","box","spoon");
something=storage[Math.floor(Math.random()*storage.length-1)];would be better if the length of storage changes.
pom :asian:
upuaut
September 11th, 2002, 03:43 AM
Smart Pom.. always thinking.
lostinbeta
September 11th, 2002, 03:46 AM
It's Pom, who would expect less:)
pom
September 11th, 2002, 04:20 AM
:blushing:
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 :P
lostinbeta
September 11th, 2002, 04:21 AM
Sorry david. Haha, it looks like im truckin' for that 1000 mark:)
upuaut
September 11th, 2002, 04:25 AM
Yes this is true. Our little upstart Lostinbeta has surpassed even my long windedness. Pretty impressive. It probebly happened because I've been spending a lot more time working and less socializing over the last two months. I still have so much to do. I get into Flash slumps (as we all do) which last a month or so.. when that happens I do a lot more posting than work. Who knows maybe I can blow him out in October. (I tend to think not though.. he's a great asset and one of the reasons I feel I can take the time to work instead of post. :) thanks again for that beta)
lostinbeta
September 11th, 2002, 04:26 AM
Oh great, now I am blushing. Thanks david:)
pom
September 11th, 2002, 04:32 AM
OK, as August's Pseudo-King of the forum, I designate Lost September's King of the forum. :)
pom :asian:
lostinbeta
September 11th, 2002, 04:38 AM
Wow, now I am blushing even more. I have never been a Pseudo-King before!:)
What are my duties as Psuedo-King of the month?
pom
September 11th, 2002, 04:46 AM
I don't know! But you could, for instance, write a tutorial about an effect you really like. I kinda did that with the perspective thing, even though I can't get it finished... :*(
Here's your mission, Pseudo-King: amaze us!
pom :asian:
lostinbeta
September 11th, 2002, 04:47 AM
Ok, I will work on that tomorrow. For now, I am off to bed, I am not tired, but it is 3:45am so I should be getting to bed.
pom
September 12th, 2002, 08:20 AM
Do you have an idea yet? :)
lostinbeta
September 12th, 2002, 12:44 PM
Actually I do have an idea. I think I am going to start simple and do one on easing. I noticed that kirupa doesn't have any tutorials on that, so I figured I could write one up.
I think easing is an important part of AS Motion, it adds a realisitic look, and a lot of people ask how to do that. So, what better than a simple effect that is effective in many ways?
upuaut
September 12th, 2002, 01:04 PM
great idea.
lostinbeta
September 12th, 2002, 01:31 PM
Thanks, I figured, I get a lot of e-mails asking me how I did my Footer, and all it uses is simple easing with mouse follow! I tell them that a couple people didn't even know what easing was.
It is the very first AS motion thing I learned in Flash (other than _x += 5 of course). I just thought it was important to know easing.
__FLATLINE__
September 12th, 2002, 03:57 PM
Cool! I would love to know easing! Can't wait for the tutorial!
pom
September 13th, 2002, 07:09 AM
Great idea! By the way, Lost, just for general culture, you can check Robert Penner's Time-based Easing Equations (http://www.robertpenner.com/index2.html) instead of the regular distance-based equations. That guy's just amazing...
pom :asian:
lostinbeta
September 13th, 2002, 12:16 PM
Hey Ilyas, I already submitted the tutorials to Kirupa, but I will check out Robert Penners site and see what I can come up with. Maybe I will update the already submitted tutorials.
pom
September 14th, 2002, 02:47 PM
Don't worry about that, I don't think it is very useful to put those in the tutorial, even as a reference. They're nice, but simply impossible to use :)
pom :asian:
Nice tutes by the way. Very clear. I like that.
lostinbeta
September 14th, 2002, 04:34 PM
Thanks Ilyas, I like your tutorials on perspective too. Very cool.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.