# Math.random

Page 1 of 1

## 1 Replies - 611 Views - Last Post: 04 November 2010 - 07:59 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=198297&amp;s=7984189d2e4aa9097cef08e63a45b69e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 <3DIC

• D.I.C Regular

Reputation: 6
• 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

• The Algorithmi

Reputation: 729
• 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

```