It's not a secret anymore!*
The moose likes Java in General and the fly likes random number gene betwn 2 no's 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 number gene betwn 2 no Watch "random number gene betwn 2 no New topic
Author

random number gene betwn 2 no's

Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
hai dudes,
i need to generate random numbers(ofcourse using Math.random()) between the 2 numbers only(say i need to generate no's betwn 1 and 50 only).how can we achieve this.

thankQ
Bye
Mahesh
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
I think , java.util.Random class is easy to use more than java.lang.Math.random() method...


So, if you want to random number between 2 number.

Example random 1 - 50



SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Somkiat Puisungnoen:

Mmm, Pui, I guess you forgot to have a look at the API that the parameter is exclusive.. So 50 won't be included in the randomized numbers... Instead of no = r.nextInt(50);, it should be no = r.nextInt(51); so that 50 is included in the randomized numbers...

Am I a bug fixer? :roll: Anyway, I hope the original poster gets what he wants correctly...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Thank you for your comment....
Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
Dear Friends,
Thanks alot for ur ans but i hope the ans u have provided is not satisfactory , becoz i need to generate random numbers between 2 numbers i.e say betwn 10 and 50 or 50 and 100 etc and not just 0 and 10,50 etc.
the solution u have given is only for numbers betwn 0 and 20,30 likewise .
Hope u got my Q
regards,
Mahesh
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
look into the '+' operator, when applied to int,int


I've heard it takes forever to grow a woman from the ground
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
http://www.xdweb.net/~dibblego/java/faq/answers.html#q13
[ September 07, 2004: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Just use Random#nextInt(int), for example:

Note that there are issues relating to initial seed values when creating a random number generator. You should read the API documentation for details.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

you need to figure out the range of possible values. if i want a number between 1 and 10, inclusive on both ends, there are 10. between 8 and 24 (inclusive) - 17.

generate a random integer that is less than the range. so in my second example, i would try to make a random number between 0 and 16 inclusive.

then, you need to figure out the offset. in my second example, my offset is 8. so, add 8 to my generated number.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random number gene betwn 2 no's