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

Edited 5 Years Ago by peter_budo: n/a

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()

}

}

Edited 5 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

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)

public int random(int min, int max) {
		int random = (int) (Math.random() * (max + 1));
		return random < min ? random(min, max) : random;
	}
This question has already been answered. Start a new discussion instead.