# method: return a random number between two integers

• (2 Pages)
• 1
• 2

## 21 Replies - 24947 Views - Last Post: 11 July 2011 - 06:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=238992&amp;s=ef9f5640b92d71a764430e992ace3e91&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

# method: return a random number between two integers

Posted 11 July 2011 - 09:18 AM

This method needs to return a random integer between int first and int second, inclusive. (first and second can be
positive or negative, and be in either order (smaller first, larger first, or equal)).

Here is my method, but one problem I noticed is that if I input 2 and 9, sometimes I get the output 10, which is clearly not between 2 and 9 inclusive. Also, what if int first and int second are equal? The method is not void, but I do not want to return a value, because there is no integer between them if they are equal?

``` public static int randomNumber(int first, int second)
{
int randomNum;
if (first<second)
{
randomNum= (int)(second*Math.random()+first);
return randomNum;
}

else if (second<first)
{
randomNum= (int)(first*Math.random()+second);
return randomNum;
}
else // first and second are equal
{
System.out.println("The two numbers are equal");
return first;
}
}
```

This post has been edited by lolitacharm: 11 July 2011 - 09:19 AM

Is This A Good Question/Topic? 0

## Replies To: method: return a random number between two integers

### #2 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 09:23 AM

randomNum= (int)(second*Math.random()+first);

Lets take the example with 2 and 9 and with Math.random() giving 0.9
randomNum = (int)(9*0.9+2) = 10.1

### #3 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 09:28 AM

doesnt randomNum= (int)(second*Math.random()+first); return a random number between first and second, inclusive?

### #4 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 09:55 AM

CasiOo, on 11 July 2011 - 09:23 AM, said:

randomNum= (int)(second*Math.random()+first);

Lets take the example with 2 and 9 and with Math.random() giving 0.9
randomNum = (int)(9*0.9+2) = 10.1

Could it be: (int)((second-1) * Math.random() + first);
or something like that?

### #5 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 09:58 AM

I would simply just use the Random class instead
```Random rnd = new Random();
randomNum = rnd.nextInt( second - first + 1 ) + first;
```

This post has been edited by CasiOo: 11 July 2011 - 10:01 AM

### #6 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:00 AM

As specified, randomNumber(4, 10) returns a number in the range (0..6), plus 4.

10* math.random() returns a float in the range (0..9.999....). Adding 4 and casting gives you an int in the range (4..13) (ie, (0..9), plus 4).

Does that help clarify?

This post has been edited by jon.kiparsky: 11 July 2011 - 10:02 AM

### #7 g00se

• D.I.C Lover

Reputation: 3536
• Posts: 16,027
• Joined: 20-September 08

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:00 AM

### #8 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:07 AM

g00se, on 11 July 2011 - 10:00 AM, said:

But with that example you will never get the max value unless you round up?

EDIT: That example really should be
```(int) Math.round( Math.random() * (max-min) + min )

```

or

```(int) (Math.random() * (max-min + 1) + min)

```

This post has been edited by CasiOo: 11 July 2011 - 10:22 AM

### #9 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:11 AM

jon.kiparsky, on 11 July 2011 - 10:00 AM, said:

As specified, randomNumber(4, 10) returns a number in the range (0..6), plus 4.

10* math.random() returns a float in the range (0..9.999....). Adding 4 and casting gives you an int in the range (4..13) (ie, (0..9), plus 4).

Does that help clarify?

I do not want to use the random class, I want to use math.random. Sorry, I'm still confused. The numbers int first and int second are entered by the user, so what was wrong with my formulas? Why won't it return a random int between int first and int second inclusive?

### #10 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:31 AM

Quote

Why won't it return a random int between int first and int second inclusive?

Break it down. You're doing three things, or really four, when you call randomNumber(i,j). First, you generate a random number n, where 0 <= n < 1 . Then, you multiply it by the larger of the two numbers provided by the user - suppose that's j, you now have a float n where 0 <= n < j . Then you add i, so you have a float n where i <= n < i+j . Finally, you cast to an int, leaving you with an int in the range (i..i+j-1).

But what you want is a number in the range (i..j), inclusive, right?

According to the spec you provided, randomNumber(4, 10) should return a number in the range (4..10). That's equivalent to (0..6), plus 4, right? So your problem is with the range you're generating.

### #11 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:31 AM

Oh! Is it:

```int randomNum;
if (first<second)
{
randomNum= (int)((second-first)*Math.random()+first);
return randomNum;
}
```

??

### #12 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:34 AM

lolitacharm, on 11 July 2011 - 10:31 AM, said:

Oh! Is it:

```int randomNum;
if (first<second)
{
randomNum= (int)((second-first)*Math.random()+first);
return randomNum;
}
```

??

Check my post above and you have your answer You will never the max

### #13 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:39 AM

but with this last code I provided, I tried input, say 2 and 9, and I get all numbers (2, 3,4,5,6,7,8,) except 9. Why is the highest number missing now?

AH! do I need to add something to first now, at the end of that?

```int randomNum;

if (first<second)

{

randomNum= (int)((second-first + 1)*Math.random()+first);

return randomNum;

}

```

?????????????????????????

Another problem I am having is that, what if two equal numbers are entered? The method is not void, so a number has to be returned, but what if I dont want a number to be returned because there is no number in between (i.e. the two numbers entered were equal)??

### #14 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:43 AM

lolitacharm, on 11 July 2011 - 10:39 AM, said:

but with this last code I provided, I tried input, say 2 and 9, and I get all numbers (2, 3,4,5,6,7,8,) except 9. Why is the highest number missing now?

AH! do I need to add something to first now, at the end of that?

```int randomNum;

if (first<second)

{

randomNum= (int)((second-first + 1)*Math.random()+first);

return randomNum;

}

```

?????????????????????????

Another problem I am having is that, what if two equal numbers are entered? The method is not void, so a number has to be returned, but what if I dont want a number to be returned because there is no number in between (i.e. the two numbers entered were equal)??

You could make it throw an exception
```public int randomNumber( int min, int max ) throws RandomNumberException {
if (...)
.....
else if (...)
.....
else
throw new RandomNumberException();
}

public class RandomNumberException extends Exception {

public RandomNumberException() {
super( "The two numbers were equal" );
}
}

```

### #15 lolitacharm

Reputation: 2
• Posts: 118
• Joined: 25-June 11

## Re: method: return a random number between two integers

Posted 11 July 2011 - 10:46 AM

OKAY, im running into another problem. I just tried -4 and -1 and the random number was 0. That is clearly not between -4 and -1!! Does this not work correctly for negative numbers??