Pseudorandom Number Generator – Selected API Classes 03/22/2021 Rivka Scheinberg Post in Implementing an Interface,Non-Static Member Classes,Oracle Exams Pseudorandom Number Generator The class Math provides the random() method for generating pseudorandom numbers of type double that are in the open interval [0.0, 1.0). The Random class (p. 482) should also be considered, as it is more versatile and easier to use. static double random() Returns a random number greater than or equal to 0.0 and less than 1.0, where the value is selected randomly from the range according to a uniform distribution. We can simulate a dice roll as follows: Click here to view code image int diceValue = 1 + (int)(Math.random() * 6.0); // A dice roll in range [1 .. 6] The dice value will always be in the interval [1, 6], depending on the pseudorandom number returned by the Math.random() method, as we can see from the sample dice rolls below. Click here to view code image Math.random(): {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}Multiply by 6: {0.0, 0.6, 1.2, 1.8, 2.4, 3.0, 3,6, 4.2, 4.8, 5.4}Convert to int: {0, 0, 1, 1, 2, 3, 3, 4, 4, 5}Add offset 1: {1, 1, 2, 2, 3, 4, 4, 5, 5, 6} 8.7 The Random Class In computer games and simulations we often need to generate a sequence of random numbers. For a dice game it is necessary to simulate rolling the dice—that is, generating a dice roll value between 1 and 6. Ideally, the probability of a given value is the same (one-sixth) for each value on a die. The numbers generated this way are called random numbers. To generate random numbers with the help of a program is not possible, as a program can only compute values, not pick them randomly. To guarantee equal probability for all cases is very difficult, so we have to settle for pseudorandom numbers—that is, a sequence of numbers that closely approximates a sequence of random numbers. A lot of research has gone into finding mathematical formulae that compute good approximations to random numbers. The java.util.Random class implements the java.util.random.RandomGenerator interface that defines the common protocol for pseudorandom number generators (PRNGs) in Java. Generating numeric streams of pseudorandom values using the Random class is covered with the discussion on creating streams (§16.4, p. 900). The following constructors can be used to create a new random number generator, optionally specifying a seed value (p. 484). Random()Random(long seed) The Random class implements PRNGs for different primitive data types. The appropriate next method can be called on a Random object to obtain the next pseudorandom value. Click here to view code image int nextInt()int nextInt(int bound)long nextLong()float nextFloat()double nextDouble()boolean nextBoolean() Return the next pseudorandom, uniformly distributed value that is either int, int between 0 and bound (exclusive), long, float between 0.0f and 1.0f (exclusive), double between 0.0d and 1.0d (exclusive), or true/false, respectively. Here we will concentrate on a pseudorandom generator for int values, but first we need to create an object of the Random class: Click here to view code image Random generator = new Random(); We can then call the method nextInt() repeatedly on this object every time we need a new random int value: Click here to view code image int number = generator.nextInt(); Each call to the method will return a random integer in the interval [–231, 231 – 1], which is the range of the int data type.