How to generate random number in java
Is there any method in java to generate random number (integer, byte or float).
please tell me if any i need it in my program.
if possible please give full example.
thanks.
aman rathi
Junior Poster in Training
61 posts since Oct 2009
Reputation Points: 13
Solved Threads: 3
Google = >> Java random number tutorial and you get this explanation of usage(at the bottom). Next time please search before you post such basic question
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
not to mention, there are a lot of examples on this forum itself.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
import java.util.Random;
public class random{
public static void main(String args[]){
System.out.println("random number generation");
Random obj= new Random(); //create object for random class
int rgen= obj.nextInt(100); //get random number with in the range of 0-100
int rgen1= obj.nextInt(1000);//get random number with in the range of 0-1000
System.out.println(rgen);
System.out.println(rgen1);
//if u want double random number use obj.nextDouble()
}
}
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
Hi balajisathya88
Please be aware that there is a policy on this forum of not just giving people the code they need for their homework. People learn best if you point them in the right direction (eg a tutorial, or the name of the class/method they need), then let them practice doing their own research and developing their own code which they will fully understand. You would have helped the OP better by saying something like "check out the Random class and its nextInt() method"
ps Your code violates standard Java naming conventions by having a lower-case class name; not such a good example,
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Hi balajisathya88
Please be aware that there is a policy on this forum of not just giving people the code they need for their homework. People learn best if you point them in the right direction (eg a tutorial, or the name of the class/method they need), then let them practice doing their own research and developing their own code which they will fully understand. You would have helped the OP better by saying something like "check out the Random class and its nextInt() method"
ps Your code violates standard Java naming conventions by having a lower-case class name; not such a good example,
k sir ,
i got it, am new donknow the rules... but am explained everylines in the above example...
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
k sir ,
i got it, am new donknow the rules... but am explained everylines in the above example...
Forum rules is first thing you should always check when joining any forum, it tell you what is expected of you and what you shouldn't do.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
ya...but if need specific range use obj.nextInt(end)+start
obj.nextInt(5)+1 (is displays random number between 1 to 5)
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
Taken that aman rathi did not come back on this for last 2 days, arguing about minor details is pointless.
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902