Java Reference
In-Depth Information
LISTING 3.2
continued
num1 = generator.nextInt(15) + 20;
System.out.println ("From 20 to 34: " + num1);
num1 = generator.nextInt(20) - 10;
System.out.println ("From -10 to 9: " + num1);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
num2 = generator.nextFloat() * 6; // 0.0 to 5.999999
num1 = ( int )num2 + 1;
System.out.println ("From 1 to 6: " + num1);
}
}
OUTPUT
A random integer: 1773351873
From 0 to 9: 8
From 1 to 10: 6
From 20 to 34: 20
From -10 to 9: -6
A random float (between 0-1): 0.71058085
From 1 to 6: 3
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 3.16 Given a Random object called rand , what does the call rand.nextInt()
return?
SR 3.17 Given a Random object called rand , what does the call
rand.nextInt(20) return?
SR 3.18 Assuming that a Random object has been created called generator ,
what is the range of the result of each of the following expressions?
a. generator.nextInt(50)
b. generator.nextInt(5) + 10
c. generator.nextInt(10) + 5
d. generator.nextInt(50) - 25
Search WWH ::




Custom Search