-
Random question!!
Hi am trying to create a program that the user defines a max and min number and flash returns a random number. I have got this working with the following script. Thanks to some poeple who answered some questions earlier.
max = 1+ Number(high);
diff=max - Number(low);
rand = Math.floor(Math.random()*(diff));
random = rand + Number(low);
I am pretty new to writing actionscript just wondered if anyone knew if there was a neater way to write this?
And also i am trying to get flash to display three random numbers at the same time instead of just one. Is there a simple way of doing this in the same text box (random)?
Thanks for any help
Much appreciated
Dan R
-
Senior Member
Here it is in a one-liner, first using the old 'random' function, which I prefer:
code:
r = random(Number(high)+1-Number(low))+Number(low);
And then using Math.random() which is a more modern parlance, but uglier.
code:
r = Math.floor(Math.random()*(Number(high)+1-Number(low)))+Number(low);
As you can see, this last version is functionally identical to yours, I've just put everything on one line and avoided the use of temporary variables which are only used once.
In general, you should note that
random(x)
is equivalent to
Math.floor(Math.random()*x)
- Jim
-
Cheers
Thanks a lot, it is really useful to see how you can write script so much simplere, i guess maybe that will come easier as i become more confident with different areas of actionscript.
Thanks
Dan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|