By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1.  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
  1.  public int nextInt(Random r, int lower, int higher) { 
  2.     int ran = r.nextInt();
  3.     double x = (double)ran/Integer.MAX_VALUE * higher;
  4.     return (int)x + lower;
  5.  }
  6.  
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
  1.  public int nextInt(Random r, int lower, int higher) { 
  2.     int ran = r.nextInt();
  3.     double x = (double)ran/Integer.MAX_VALUE * higher;
  4.     return (int)x + lower;
  5. }
  6.  
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
  1.   Random r=new Random();
  2.    int n=(r.nextInt(101) + 100);
  3.  
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
  1.  public int nextInt(Random r, int lower, int higher) { 
  2.     int ran = r.nextInt();
  3.     double x = (double)ran/Integer.MAX_VALUE * higher;
  4.     return (int)x + lower;
  5.  }
  6.  
Bzzzt! Thanks for playing; close but no cigar ;-)

Better make that line:
Expand|Select|Wrap|Line Numbers
  1.     double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
  2.  
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
  1.     double x = (double)ran/Integer.MAX_VALUE * (higher-lower);
  2.  
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
  1.  int x = 100 + (int)(Math.random()*100);
Oct 30 '12 #13

P: 1
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2. public class Question {
  3.  
  4.  
  5.     public static void main(String[] args) {
  6.       Random random = new Random ();
  7.          int x = 100 + (int)(Math.random()*200);
  8.             System.out.println(Math.round(Math.random()*200));
  9.     }
  10.  
  11. }
Jan 18 '13 #14

Post your reply

Sign in to post your reply or Sign up for a free account.