D.9 Introduction to Random-Number Generation
We now take a brief diversion into a popular type of programming application—simulation and game playing. In this and the next section, we develop a nicely structured game-playing program with multiple methods. The program uses most of the control statements presented thus far in the appendices and introduces several new programming concepts.
Random numbers can be introduced in a program via an object of class Random
(package java.util
) or via the static
method random
of class Math
. A Random
object can produce random boolean
, byte
, float
, double
, int
, long
and Gaussian values, whereas Math
method random
can produce only double
values in the range 0.0
≤ x
< 1.0
, where x is the value returned by method ...
Get Android How to Program, 3/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.