A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: restricting random

  1. #1
    Junior Member
    Join Date
    Mar 2002
    Posts
    4
    greetings,
    i need to create a random whole number between two numbers such as,
    between 7 and 25.

    does anyone know how this is done?

    -jason


  2. #2
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252
    to create a random integer between 7 and 25.

    Math.random() returns a random # between 0 and .99999
    so first we stretch the span
    18*(Math.random());
    which changes the span to 0 to 17.9999
    so then add 7.5 and chop the decimal.
    Math.floor(18*(Math.random())+7.5);
    so this makes the range 7 to 25, just the integers.

    in general:
    a to b, just the integers

    Math.floor((b-a)*(Math.random())+a+.5);

    just in case:

    are you trying to get something like 6d4+1 ?

    because that's different.

    six four sided dice plus 1 makes a nice bell curve centered on 16, to get this sort of curve, just add up 6 separately generated a=1,b=4 types, then add 1.

    you'll notice a lack of >21 <11 results, try it....

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