438,557 Members | 1,184 Online Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

# Random numbers between number range in Java

 P: 20 if i use java.util.Random 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) Jan 12 '07 #1
13 Replies

 10K+ P: 13,264 if i use java.util.Random 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. Expand|Select|Wrap|Line Numbers  int x = 100 + (int)(Math.random()*200); Jan 12 '07 #2

 P: 20 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 Jan 12 '07 #3

 10K+ P: 13,264 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 Expand|Select|Wrap|Line Numbers  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;  }   Jan 12 '07 #4

 10K+ P: 13,264 You would have to write your own custom method for it. Something like Expand|Select|Wrap|Line Numbers  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 Jan 12 '07 #5

 P: 20 I got the soltion from custom method. Thanks for giving the better solution. Jan 12 '07 #6

 Expert 100+ P: 1,510 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 Expand|Select|Wrap|Line Numbers   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 Jan 12 '07 #7

 P: 2 if i use java.util.Random 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.println(Math.round(Math.random()*100+10 0)); } } Vladimir1 Jan 20 '07 #8

 P: 1 if i use java.util.Random 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.Random; public class Random2 extends Random{ public int nextInt(int lower,int upper){ return nextInt((upper-lower+1))+lower; } } Oct 5 '07 #9

 Expert 10K+ P: 11,448 You would have to write your own custom method for it. Something like Expand|Select|Wrap|Line Numbers  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: Expand|Select|Wrap|Line Numbers     double x = (double)ran/Integer.MAX_VALUE * (higher-lower);   Using "nextInt(higer-lower)+lower" is also an option. kind regards, Jos kind regards, Jos Oct 6 '07 #10

 10K+ P: 13,264 Bzzzt! Thanks for playing; close but no cigar ;-) Better make that line: Expand|Select|Wrap|Line Numbers     double x = (double)ran/Integer.MAX_VALUE * (higher-lower);   Using "nextInt(higer-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. Oct 6 '07 #11

 Expert 10K+ P: 11,448 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 Oct 6 '07 #12

 P: 1 @r035198x This should be the following: Expand|Select|Wrap|Line Numbers  int x = 100 + (int)(Math.random()*100); Oct 30 '12 #13

 P: 1 Expand|Select|Wrap|Line Numbers 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));     }   } Jan 18 '13 #14 