Java Reference
In-Depth Information
Math.abs( -2 ) returns 2
Math.abs( -2.1 ) returns 2.1
5.10.5 The random Method
You have used the random() method to generate a random double value greater than or
equal to 0.0 and less than 1.0 ( 0 <= Math.random() < 1.0 ). This method is very useful.
You can use it to write a simple expression to generate random numbers in any range. For
example,
Returns a random integer
between 0 and 9
( int ) ( Math.random ( ) * 10 )
Returns a random integer
between 50 and 99
50 + ( int ) ( Math.random ( ) * 50 )
In general,
Returns a random number between
a and a + b, excluding a + b
a + Math.random ( ) * b
Tip
You can view the complete documentation for the Math class online at download
.oracle.com/javase/7/docs/api , as shown in Figure 5.7.
Note
Not all classes need a main method. The Math class and the JOptionPane class do
not have main methods. These classes contain methods for other classes to use.
F IGURE 5.7
You can view the documentation for the Java API online.
 
 
Search WWH ::




Custom Search