954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
Moderator
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
Moderator
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
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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.

thanatos1
Light Poster
34 posts since Jul 2009
Reputation Points: 7
Solved Threads: 3
 

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
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
public int random(int min, int max) {
		int random = (int) (Math.random() * (max + 1));
		return random < min ? random(min, max) : random;
	}
Jaydenn
Light Poster
41 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

i don't think we're even arguing, i told him he's wrong and it's pretty much a fact.

thanatos1
Light Poster
34 posts since Jul 2009
Reputation Points: 7
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: