It's not a secret anymore!
The moose likes Java in General and the fly likes Random Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Random Numbers" Watch "Random Numbers" New topic

Random Numbers

Noman Iqbal
Ranch Hand

Joined: Jun 25, 2001
Posts: 100
A-O-A Dear Friends,
I wanna know about that how to generate a random number
between two given numbers. for example I want to generate a random
number between 40 and 60, how to do it ? what function of which class
I can use ? please help me out. If u can send me a simple example
then it will be better. I also want the class hierarchy of I/O
Streams. Waiting for your reply.
Noman Iqbal.

karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
well, try java.lang.Math.random(). this returns a pseudo-random number x with 0 <= x < 1.
the only thing you need to to is multiply/round/add/subtract it according to your needs (eg random number x, with a <= x < b:
double d = Math.random();
int myRandomNumber = a + 1 + Math.floor(d * (b-a));
// equal chances for every integer between a and b
// am i right ?
well thats for the random number (did not test the code).
the io streams are described in the java api description you can find at if you do not already have it.
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
The API is available here

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Please don't post the same message to more than one forum. You obviously expect to read more than one forum - so do the people who might answer your question.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
The method proposed by Karl isn't quite right; the smallest value that it can return is a + 1, not a. (In fairness, he said right out that he didn't test the code.)
That said, there's a much better way to get a random integer between a and b: user Random.nextInt(int n), which returns a random integer from 0 (inclusive) to n, exclusive. Here's a method to return a random integer from a to b, inclusive:
private static Random random = new Random();
static int rnd(int min, int max) {
return random.nextInt(max - min + 1) + min;
Note that it's much more common in Java to express ranges from min, inclusive, to max, exclusive. To make the method obey this convention, simply eliminate the + 1.
A great advantage of this method over Karl's is that it is easy to prove that it works (i.e., returns each of the integers in the range with equal likelihood). This is because the method takes advantage of a library method that does the hard work for you (Random.nextInt(int)). A second advantage is that it's much faster. I discuss this in more detail in Item 30 of my book ("Know and use the libraries").

Joshua Bloch
Author of Effective Java

Joshua Bloch <br />Author of <a href="" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="" target="_blank" rel="nofollow">Java Puzzlers</a>
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388

uuups. yes joshua is right. got that wrong. and i didnt know about the getNexitInt() method of Random. this is sure way better than my proposal.
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
I have noticed that using Random r.nextDouble() is much faster than using Math.random(). Why is that? I would have thought that calling a static method would be faster than creating an object and then calling the getDouble method, but I tested both of these and the Random class was the clear winner. Anyone know why? -- Chad
Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
If you look at the source for the Math.random() method, you'll notice that all it does is create an instance of Random and then return r.nextDouble(). This may explain the difference (though you'd think they'd be the same speed).
Angelo Watson
Ranch Hand

Joined: Oct 27, 2002
Posts: 39
if you use the nextInt() method how can you display a unique number, that doesnt display itself again.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You can't, unless you use something like the method(s) suggested in your earlier post. You may well use nextInt() for some steps within that solution (assuming you don't simply use Collections.shuffle()) but that shouldn't be a problem.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Random Numbers
It's not a secret anymore!