Java Reference
In-Depth Information
Method
Returns
nextInt(n)
A random integer between the integers 0 (inclusive) and n
(exclusive)
nextDouble()
A random floatingÉ°point number between 0) (inclusive) and 1
(exclusive)
For example, you can simulate the cast of a die as follows:
Random generator = new Random();
int d = 1 + generator.nextInt(6);
The call generator.nextInt(6) gives you a random number between 0 and 5
(inclusive). Add 1 to obtain a number between 1 and 6.
To give you a feeling for the random numbers, run the following program a few times.
Figure 3
The Buffon Needle Experiment
256
257
ch06/random1/Die.java
1 import java.util.Random;
2
3 /**
4This class models a die that, when cast, lands on a random
5face.
6 */
7 public class Die
8 {
9 /**
10Constructs a die with a given number of sides.
Search WWH ::




Custom Search