# THE RANDOM NUMBER CLASS

## 1. THE RANDOM CLASS

In earelier work on random numbers we wrote our own random number generator and looked at the random number generator available in the Math class. Inspection of the Java API documentation indicates that there also a class Random which has in it a method nextInt() which returns the next number in a "pseudo" random number sequence (the seed for which is provided by the system clock). The number returned is an integer which can take any value within the default range defined for this numeric type. A random number application class which uses this method is presented in Table 1 below.

 ```// RANDOM NUMBER APPLICATION 2 // (Using built-in generator supplied by the JDK) // Frans Coenen // Monday 21 June 1999 // The University of Liverpool, UK import java.io.*; import java.util.*; class RandomApp2 { /* Main method */ public static void main(String[] args) throws IOException { long ranNum = 10; final long OFFSET = ((long)Integer.MIN_VALUE) * -1l; // 2147483648 final long DIVISOR = ((long)Integer.MAX_VALUE) + OFFSET + 1l; // 4294967295 Random newRandom; // Create an instance of the class Random. newRandom = new Random(); // Generate random numbers until a random number equivalent to // 50 has been produced. while (ranNum != 50l) { ranNum = ((long) newRandom.nextInt()); ranNum = ((ranNum+OFFSET) * 100l)/DIVISOR; System.out.print(ranNum + " "); } // End System.out.println(); } } ```

Table 1: Source code for random number application class (version2)

Points to Note

 The 'l' character appended to the numbers -1, 1, 50 and 100 in the above code tells the Java compiler that these numbers should be interpreted as being of type long and not the default int interpretation that might take place otherwise. (The character 'f' can be used in a similar manner to tell Java that a particular floating point number should be interpreted as being of type float andv not the default double.) The random number returned has a value within the default maximum and minimum defined for a Java integer (-2,147,483,648 to 2,147,483,647). We wish to return a positive number thus we add the OFFSET 2,147,483,648 to the number returned. However the result may now exceed to permitted range for a Java integer. Consequently we define ranNum as being of type long and cast the integer returned by the nextInt method to be a long integer.

Some example output produced by the above code is given in Table 5.

 ```\$ java RandomApp2 94 2 61 87 82 97 91 41 88 91 59 92 83 94 12 91 65 87 68 68 82 2 8 38 94 34 80 91 85 18 17 13 69 90 47 66 32 47 9 65 54 59 18 21 97 63 29 43 12 26 6 49 84 89 29 25 71 16 17 77 55 24 19 98 8 13 92 1 96 73 75 27 87 53 52 39 80 81 25 81 13 72 0 73 53 33 85 94 55 98 3 89 12 53 8 51 2 3 48 58 65 67 52 53 93 89 52 89 50 \$ java RandomApp2 12 74 67 41 73 6 29 1 77 4 73 40 77 88 95 84 28 31 28 55 56 51 85 0 60 20 38 52 96 19 49 2 34 55 25 67 60 40 92 76 2 86 84 8 84 25 77 65 43 56 40 64 66 29 55 35 3 39 67 97 70 4 90 40 85 0 13 69 0 55 63 39 45 91 10 73 73 11 83 12 74 81 67 69 13 81 18 65 95 95 42 51 74 38 26 11 36 7 76 33 37 5 47 16 52 1 12 30 60 94 6 2 56 20 59 64 56 26 35 65 16 35 84 59 87 50 \$ java RandomApp2 90 83 28 41 65 13 44 21 50 ```

Table 5: Sample output from random number application class (version 2).

Created and maintained by Frans Coenen. Last updated 15 September 2000