1 Replies - 386 Views - Last Post: 04 November 2010 - 07:59 AM Rate Topic: -----

#1 <3DIC  Icon User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Math.random

Posted 04 November 2010 - 07:15 AM

Why is it that this statement produces a Min: -4 Max: 2?

(int)(Math.random() * 8 + -5)


i thought the min was the 2nd number was min, i.e -5 not -4
what is going on here?

This post has been edited by <3DIC: 04 November 2010 - 07:17 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Math.random

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Math.random

Posted 04 November 2010 - 07:59 AM

there's nothing wrong, the minimum value is -5, it probably doesn't get there too often because the range of values >= 0.00 and < 1 is pretty large.

try casting Math.random()*8 first before subtracting 5 instead
(int)(Math.random() * 8) + -5


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1