A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Math gurus, please help!

  1. #1
    ?
    Join Date
    Feb 2002
    Location
    sweden
    Posts
    80
    Could someone good at programming and math take a look at the 'Math.random()' thread in this forum and explain how it really works...? Please? Sorry if this is concidered cross posting but it's only the same people batteling it out over there... Could be good if someone else had a look too...

    .b

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    248
    I believe elstudion is right about using Math.floor. It's a somewhat complicated subject, but I'll try to make it as simple as possible.
    Math.random() generates a (relatively) random floating-point number between 0 and .9999. We know that anything * 1 = that number itself. So Math.random() * 1 = a random floating point number from 0 to .9999, NOT 0 to 1. So you can see that if we take our random number and multiply by 10, we should get a number from 0 to 9.999. If we use Math.round(), we end up with a number from 0 to 10, but with an uneven distribution (check out elstudion's posts to see evidence).

    Therefore, what we want to do is use math.floor(). That way, we really do get a number from 0 to 9. Then anything you ADD to the end fo the equation will slide the scale up or down. For instance Math.floor(Math.random() * 10) gives you 0-9. But Math.floor(Math.random() * 10) + 10 gives you 10-19.

    I hope that makes a little sense!
    -Dan

  3. #3
    Senior Member
    Join Date
    May 2002
    Posts
    248
    haha I didn't even notice that you're the one who made this post here, elstudion! I think you're right with the Math.floor().

    -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