A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Random question!!

  1. #1
    Junior Member
    Join Date
    Sep 2003
    Location
    worthing
    Posts
    19

    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

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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

  3. #3
    Junior Member
    Join Date
    Sep 2003
    Location
    worthing
    Posts
    19

    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center