Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Random numbers - API

[an error occurred while processing this directive]

Two classes. Java provides the Math.random() method as well as the java.util.Random class. The methods of the Random class often produce random numbers in a more convenient form, but requires creating an object, which sometimes is inconvenient. In constrast, the Math.random() method produces a double value which must sometimes be translated and cast into the form you need it. It's a tradeoff between the globalness of Math.random more directly useful numbers from the Random class.

java.util.Random class

To use the Random class create an object of this class (giving a seed to the constructor if you wish), then call one of the methods below to get a new random number. You must use one of the following import statements:

   import java.util.Random; // Only the Random class
   import java.util.*;      // All classes in the java.util package

Random constructors

   Random r = new Random();          // Default seed comes from system time.
   Random r = new Random(long seed); // For reproducible testing

Random methods

The most common methods are those which return a random number. These methods return a uniform distribution of values, except nextGaussian(). In these examples, x is a Random object.

Return
type
CallDescription
int i = r.nextInt(int n)Returns random int >= 0 and < n
int i = r.nextInt() Returns random int (full range)
long l = r.nextLong() Returns random long (full range)
float f = r.nextFloat() Returns random float >= 0.0 and < 1.0
double d = r.nextDouble() Returns random double >=0.0 and < 1.0
boolean b = r.nextBoolean() Returns random double (true or false)
double d = xrnextGaussian()Returns random number with mean 0.0 and standard deviation 1.0

Example: Generating a random Color

To create any color with values for red, green, and blue (the RGB system) between 0-255:

Random r = new Random();
Color  c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));

Although the three calls to nextInt(256) look the same, each returns an independent random number.

Example: Generating a number from 1 to 6

Because nextInt(6) returns a number from 0-5, it's necessary to add 1 to scale the number into the range 1-6,

static Random randGen = new Random();
int spots;
. . .
spots = randGen.nextInt(6) + 1;

Math.random() method

The Math.random() method returns random double numbers in the range >=0.0 to <1.0 . It returns the same result as the Random nextDouble() method (see below).

double x;
x = Math.random(); // assigns random number to x

Usually the range 0.0...0.999999+ isn't what is desired so it's necessary to scale the range by multiplying and translate the values by addition. Finally, an int is commonly desired, so casting is required.

For example, if you need an int int the range 1 to 10, the following code could be used.

int n = (int)(10.0 * Math.random()) + 1;

The multiplication scales the range to 0.0 to 9.9999+, casting it to int to gets it into the integer range 0 to 9 (truncation, not rounding), then addition of 1 translates it into the range 1 to 10.

The java.util.Random class has convenient methods which do this extra work for you, but you have to create a Random object and make sure it's accessible where you need it (sometimes awkward).

Related pages

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.