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.
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.
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
not to mention, there are a lot of examples on this forum itself.
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()
}
}
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,
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...
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.
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() } }
rand.nextInt(100) goes from 0 to 99... not to 100.
ya...but if need specific range use obj.nextInt(end)+start
obj.nextInt(5)+1 (is displays random number between 1 to 5)
Taken that aman rathi did not come back on this for last 2 days, arguing about minor details is pointless.
public int random(int min, int max) {
int random = (int) (Math.random() * (max + 1));
return random < min ? random(min, max) : random;
}
i don't think we're even arguing, i told him he's wrong and it's pretty much a fact.
repeating answers is not a valid reason to revive a dead thread.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.