Java and Random Functions

104 9

    Pseudo-Random

    • Java functions to generate random numbers do not actually result in values that are genuinely random. This is often the case with computer programming languages, since genuine randomness is difficult to achieve within a program. Java instead uses pseudo-randoms, which appear random to users. Good pseudo-random methods, such as those used in Java, come close to the values you would get with a genuine random function, so they are generally adequate for most application functions.

    Math Random Method

    • The Java Math class provides a method to return a random number. The random method of the Math class returns a value of primitive type double. The returned value is a positive number between 0.0 and 1.0, by default. The following code demonstrates using the Math class to gain a random number within a Java program:

      double randNum = Math.random();

      If a programmer wants the value to be between one and ten, the following structure performs this amendment:

      double randNum = Math.random()*10;

      The result can also be cast to an int value as follows:

      int randInt = (int)randNum;

      A program may require an integer in cases where the random number is going to be used to access an array element.

    Random Class

    • The Random class provides more extensive methods for using random numbers in a Java program. Java classes intending to use the Random class can import it as follows:

      import java.util.Random;

      Once imported, programs can create a Random object as follows:

      Random randGen = new Random();

      The Random object is a random number generator, with methods to generate various types of value. To generate an int value within a certain range, programs can use the following code:

      int randomInt = randGen.nextInt(10);

      This code generates a random int value between zero and 9. The parameter indicates the top end of the desired range, but it is exclusive, so the highest value with a parameter of 10 will be 9. The Random class also provides methods to return random numbers of types double, float and long.

    Use Of Random Numbers

    • Java programs can use numbers generated from random functions in any way they might use other numerical values. A common use involves accessing a random element in an array, with the array index being chosen by the random number generation process. The following example demonstrates writing out a randomly selected value from an array:

      Random indexGenerator = new Random();

      String[] someWords = {"apple", "banana", "pear", "orange"};

      int randomIndex = indexGenerator(someWords.length);

      System.out.println(someWords[randomIndex];

      The index chosen will be within the array range because the program is passing the array length as parameter to the "nextInt" method.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.