Generalized Scaling and Shifting of Random Numbers
Previously, we simulated the rolling of a six-sided die with the statement
int face = 1 + randomNumbers.nextInt(6);
This statement always assigns to variable face
an integer in the range 1
≤ face
≤ 6
. The width of this range (i.e., the number of consecutive integers in the range) is 6
, and the starting number in the range is 1
. In the preceding statement, the width of the range is determined by the number 6
that’s passed as an argument to SecureRandom
method nextInt
, and the starting number of the range is the number 1
that’s added to randomNumbers.nextInt(6)
. We can generalize this result as
int number
= shiftingValue + randomNumbers.nextInt( ...
Get Java™ How To Program (Early Objects), Tenth Edition 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.