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

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


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Summary: Math.random() and java.util.Random

Math.random() method

 

In this section we will learn about java.util.Random and Math.random() functions.

 

A standard way to generate random numbers is to use the Math.random() method, which returens a double value in the range 0.0 up to, but not including 1.0. You can used multiplication to expand the range, addition to shift the range, and casting to convert it to an integer.

 

The advantage of this method over the Random class (see below) is that there is no need to create an object. The disadvantage is that you often have to do more work to translate the return value into a range you want.

 double Math.random()

Returns a number x in the range, 0.0 <= x < 1.0.

java.util.Random Class

 

The java.util.Random class provides more flexible ways to generate uniformly distributed random numbers, providing easy generation of types other than double, as well as providing a Gaussian distribution.


Random Constructors
Random r =new Random(); Uses time in milliseconds as the seed.
Random r = new Random(long seed); Uses the provided seed for testing purposes.
Random Methods
All methods return a uniform distribution of values, except nextGaussian(). Assume r is a Random object..
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 boolean (true or false).
double d = r.nextGaussian() Returns random double with mean 0.0 and standard deviation 1.0.
Copyright 2005 Fred Swartz
Ask programming questions?

 

 

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

 

 
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.

[an error occurred while processing this directive]