Random numbers between number range in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prileep
    New Member
    • Jan 2007
    • 20

    Random numbers between number range in Java

    if i use java.util.Rando m i can create random numbers.

    But how do i can define a Random object that its methods like
    nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by prileep
    if i use java.util.Rando m i can create random numbers.

    But how do i can define a Random object that its methods like
    nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
    Math.random() will do the job for you there.

    Code:
     int x = 100 + (int)(Math.random()*200);

    Comment

    • prileep
      New Member
      • Jan 2007
      • 20

      #3
      More specific

      Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by prileep
        Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations
        You would have to write your own custom method for it.
        Something like
        Code:
         public int nextInt(Random r, int lower, int higher) { 
        	int ran = r.nextInt();
        	double x = (double)ran/Integer.MAX_VALUE * higher;
        	return (int)x + lower;
         }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by r035198x
          You would have to write your own custom method for it.
          Something like
          Code:
           public int nextInt(Random r, int lower, int higher) { 
          	int ran = r.nextInt();
          	double x = (double)ran/Integer.MAX_VALUE * higher;
          	return (int)x + lower;
          }
          This will return values from lower to higher + lower so you will need to scale it again

          Comment

          • prileep
            New Member
            • Jan 2007
            • 20

            #6
            I got the soltion from custom method.
            Thanks for giving the better solution.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by prileep
              Ok i got the result with ur solnution. But i wanted to know whether i can set a random object to return value between a specified lower limit and upper limit so that i need not to worry about the complexity of calculations
              another alternative
              Code:
                Random r=new Random();
                 int n=(r.nextInt(101) + 100);
              r.nextInt(101) returns a value between 0 and 100 and you then add 100

              Comment

              • Vladimir1
                New Member
                • Jan 2007
                • 2

                #8
                Originally posted by prileep
                if i use java.util.Rando m i can create random numbers.

                But how do i can define a Random object that its methods like
                nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)
                Hello, friend,
                Try the following, plz:

                public class Ex {
                public static void main(String[] args) {
                System.out.prin tln(Math.round( Math.random()*1 00+100));
                }
                }

                Vladimir1

                Comment

                • jfivekiller
                  New Member
                  • Oct 2007
                  • 1

                  #9
                  Originally posted by prileep
                  if i use java.util.Rando m i can create random numbers.

                  But how do i can define a Random object that its methods like
                  nextInt() should return between two specified numbers(Eg: i want to create random numbers between 100 and 200)

                  Here was my simple solution to random numbers in a range

                  import java.util.Rando m;
                  public class Random2 extends Random{

                  public int nextInt(int lower,int upper){
                  return nextInt((upper-lower+1))+lower ;

                  }
                  }

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    You would have to write your own custom method for it.
                    Something like
                    Code:
                     public int nextInt(Random r, int lower, int higher) { 
                    	int ran = r.nextInt();
                    	double x = (double)ran/Integer.MAX_VALUE * higher;
                    	return (int)x + lower;
                     }
                    Bzzzt! Thanks for playing; close but no cigar ;-)

                    Better make that line:
                    Code:
                    	double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
                    Using "nextInt(hi ger-lower)+lower" is also an option.

                    kind regards,

                    Jos

                    kind regards,

                    Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by JosAH
                      Bzzzt! Thanks for playing; close but no cigar ;-)

                      Better make that line:
                      Code:
                      	double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
                      Using "nextInt(hi ger-lower)+lower" is also an option.

                      kind regards,

                      Jos

                      kind regards,

                      Jos
                      Did I do that?

                      Thanks for digging up this thread guys. Otherwise my mistake would not have been noticed.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        Did I do that?

                        Thanks for digging up this thread guys. Otherwise my mistake would not have been noticed.
                        I didn't dig up anything; I'm innocent as always; fivekiller found it; good work ;-)

                        kind regards,

                        Jos

                        Comment

                        • sajib99
                          New Member
                          • Oct 2012
                          • 1

                          #13
                          Originally posted by r035198x
                          Math.random() will do the job for you there.

                          Code:
                           int x = 100 + (int)(Math.random()*200);
                          This should be the following:
                          Code:
                           int x = 100 + (int)(Math.random()*100);

                          Comment

                          • Ungihan
                            New Member
                            • Jan 2013
                            • 1

                            #14
                            Code:
                            import java.util.Random;
                            public class Question {
                            
                            	
                            	public static void main(String[] args) {
                            	  Random random = new Random ();
                            		 int x = 100 + (int)(Math.random()*200);
                            			System.out.println(Math.round(Math.random()*200));
                            	}
                            
                            }
                            Last edited by Rabbit; Jan 18 '13, 06:46 PM. Reason: Please use code tags when posting code.

                            Comment

                            Working...