This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Numbers

 
Noman Iqbal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API is available here
http://java.sun.com/j2se/1.3/docs/api/index.html
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use the nextInt() method how can you display a unique number, that doesnt display itself again.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic