This week's book giveaways are in the Java EE and JavaScript forums. We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line! See this thread and this one for details.

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. bye Noman Iqbal.

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

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.

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

Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>

karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388

posted

0

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

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

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

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.