method: return a random number between two integers

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 11630 Views - Last Post: 11 July 2011 - 06:26 PM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • 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

Your formula is wrong :)
Was This Post Helpful? 1
  • +
  • -

#3 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 lolitacharm  Icon User is offline

  • D.I.C Head

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

Re: method: return a random number between two integers

Posted 11 July 2011 - 09:55 AM

View PostCasiOo, 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

Your formula is wrong :)



Could it be: (int)((second-1) * Math.random() + first);
or something like that?
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • 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 :D
Random rnd = new Random();
randomNum = rnd.nextInt( second - first + 1 ) + first;

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

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7565
  • View blog
  • Posts: 12,697
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: method: return a random number between two integers

Posted 11 July 2011 - 10:00 AM

http://technojeeves....-value-in-range
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: method: return a random number between two integers

Posted 11 July 2011 - 10:07 AM

View Postg00se, 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

Was This Post Helpful? 1
  • +
  • -

#9 lolitacharm  Icon User is offline

  • D.I.C Head

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

Re: method: return a random number between two integers

Posted 11 July 2011 - 10:11 AM

View Postjon.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?
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7565
  • View blog
  • Posts: 12,697
  • 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.
Was This Post Helpful? 1
  • +
  • -

#11 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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;
        }


??
Was This Post Helpful? 0
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: method: return a random number between two integers

Posted 11 July 2011 - 10:34 AM

View Postlolitacharm, 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
Was This Post Helpful? 1
  • +
  • -

#13 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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;

        }





????????????????????????? :D

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)??
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: method: return a random number between two integers

Posted 11 July 2011 - 10:43 AM

View Postlolitacharm, 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;

        }





????????????????????????? :D

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" );
	}
}


Was This Post Helpful? 1
  • +
  • -

#15 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2