473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Random numbers between number range in Java

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 61478
r035198x
13,262 8TB
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
prileep
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
r035198x
13,262 8TB
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
r035198x
13,262 8TB
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
prileep
20
I got the soltion from custom method.
Thanks for giving the better solution.
Jan 12 '07 #6
horace1
1,510 Expert 1GB
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
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
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
JosAH
11,448 Expert 8TB
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
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
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
@r035198x
This should be the following:
Expand|Select|Wrap|Line Numbers
  1.  int x = 100 + (int)(Math.random()*100);
Oct 30 '12 #13
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

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

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
3
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
1
by: johntilster | last post by:
How do I get uniform random numbers in the log domain? I need a random number in the log domain between -infinity and -0.69314718, which is equivalent to a range from 0 to 0.5. I usually use...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
4
by: carlos123 | last post by:
i need a formula to generate 100 random numbers 1-20 any suggestions?
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.