Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Random Numbers

Noman Iqbal
Ranch Hand
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
then it will be better. I also want the class hierarchy of I/O
bye
Noman Iqbal.

karl koch
Ranch Hand
Posts: 388
hi,
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 www.javasoft.com if you do not already have it.
karl

rani bedi
Ranch Hand
Posts: 358
The API is available here
http://java.sun.com/j2se/1.3/docs/api/index.html

Frank Carver
Sheriff
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.

Joshua Bloch
Author and "Sun God"
Ranch Hand
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:
<pre>
private static Random random = new Random();
static int rnd(int min, int max) {
return random.nextInt(max - min + 1) + min;
}
</pre>
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

karl koch
Ranch Hand
Posts: 388
hi,

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.
karl

Ranch Hand
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
author
Ranch Hand
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).
Sean

Angelo Watson
Ranch Hand
Posts: 39
if you use the nextInt() method how can you display a unique number, that doesnt display itself again.

Jim Yingst
Wanderer
Sheriff
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.

 Don't get me started about those stupid light bulbs.