Generating the Same Sequence of Pseudorandom Numbers – Selected API Classes

Generating the Same Sequence of Pseudorandom Numbers

The way in which we have used the pseudorandom number generator up to now cannot guarantee the same sequence of pseudorandom numbers each time the program is run. This is because the pseudorandom number generator is based on the time of the system clock. This is obviously different each time the program is run. If we want to generate the same sequence of pseudorandom numbers each time the program is run, we can specify a seed in the call to the Random constructor:

Click here to view code image

Random persistentGenerator = new Random(31);

In the declaration above, the seed is the prime number 31. The seed is usually a prime number (i.e., a number that is only divisible by itself or one), as such numbers are highly suitable for implementing good, viable pseudorandom number generators.

8.8 Using Big Numbers

There is a need for greater precision in arithmetic calculations than the precision offered by arithmetic operators performing calculations on finite-precision values of numeric primitive types. This is especially true for calculations involving financial or monetary values, where accumulation of rounding errors and precision is paramount. The classes BigInteger and BigDecimal in the java.math package support arbitrary-precision integers and arbitrary-precision decimal numbers, respectively, and provide methods for arbitrary-precision computations (Table 8.4). Both classes extend the java.lang.Number abstract class (Figure 8.1).

Table 8.4 Big Number Classes

Big number classes in the java.math packageDescription
BigDecimal extends NumberA class that represents immutable, arbitrary-precision, signed decimal numbers
BigInteger extends NumberA class that represents immutable, arbitrary-precision signed integers

A BigDecimal is represented as an arbitrary-precision unscaled integer value and a 32-bit integer scale. For a non-negative unscaled value, the scale represents the number of digits to the right of the decimal point. For a negative unscaled value, the number represented is (unscaled value * 10-scale). For example:

Click here to view code image

The BigDecimal  3.14   has unscaled value =  314   and scale = 2.
The BigDecimal -3.1415 has unscaled value = -31415 and scale = 4.

The BigDecimal class can represent very large and very small decimal numbers with very high precision. The class provides methods that allow control over scale and rounding behavior for arithmetic operations. In addition, the class provides support for converting to different representations and comparing BigDecimal values.

Formatting, parsing, and rounding of BigDecimal values is covered with formatting of numbers and currency (§18.5, p. 1116).

A BigInteger represents an arbitrary-precision signed integer value in base 10 and two’s-complement notation, which is internally implemented by an arbitrary-size int array. The BigInteger class has methods analagous to the BigDecimal class for arithmetic calculations, conversions, and comparison.

Here we will only provide a few examples of using the BigDecimal class. The API for the BigDecimal and BigInteger classes in the java.math package should be consulted, both for details on features presented here and additional features provided for high-precision manipulation of big numbers.

BigDecimal Constants

The class BigDecimal provides the following ready-made constants corresponding to the decimal values 0, 1, and 10, that are represented with a scale of 0:

BigDecimal.ZERO
BigDecimal.ONE
BigDecimal.TEN

Leave a Reply

Your email address will not be published. Required fields are marked *