Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor. Here is an example: import java.util.Random; public class Exercise { public static void main(String[] args) { Random rndNumbers = new Random(20); int rndNumber = rndNumbers.nextInt(); System.out.println("Number: " + rndNumber); } } Here is one example of running the program: Number: 375271809 Here is another example of running the same program: Number: 375271809 Notice that the numbers are the same. Consider this program also: import java.util.Random; public class Exercise { public static void main(String[] args) { Random rndNumbers = new Random(20); int rndNumber = 0; for (int nbr = 1; nbr < 5; nbr++) { rndNumber = rndNumbers.nextInt(); System.out.println("Number: " + rndNumber); } } } Here is one example of running the program: Number: 375271809 Number: 1472524622 Number: 1605850688 Number: 1776011503 Here is another example of running the same program: Number: 375271809 Number: 1472524622 Number: 1605850688 Number: 1776011503 Notice that the sequences are the same. In both cases, this indicates that, if you specify a seed, the Random class would generate the same number or the same sequence of numbers. Besides the second constructor, to specify the seed, you can call the setSeed() method. Its syntax is: void setSeed(long seed); This method takes one argument as the seed. Here is an example of calling it: import java.util.Random; public class Exercise { public static void main(String[] args) { Random rndNumbers = new Random(); rndNumbers.setSeed(20); int rndNumber = rndNumbers.nextInt(); System.out.println("Number: " + rndNumber); } }
So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. The Random class can assist with this. Using the Random class, you can generate random positive numbers from 0 (included) to a maximum (excluded) of your choice. To support this, the Random class is equipped with another version of the nextInt() method whose syntax is: public int nextInt(int maxValue); The argument to pass to the method determines the highest integer that can be generated by the nextInt() method. The method returns an integer. Here is an example that generates random numbers from 0 to 20: import java.util.Random; public class Exercise { public static void main(String[] args) { Random rndNumbers = new Random(); int rndNumber = 0; for (int nbr = 1; nbr < 9; nbr++) { rndNumber = rndNumbers.nextInt(20); System.out.println("Number: " + rndNumber); } } } Here is an example of running the program: Number: 1 Number: 7 Number: 1 Number: 16 Number: 14 Number: 19 Number: 3 Number: 1 The above version of the Next() method generates numbers starting at 0.
Besides the Random class, the Math class is equipped with a method that can be used to generate random numbers. The syntax of this method is: static double random(); When called, this method produces a randomly selected decimal number between 0.00 (included) and 1.00 (excluded). |
|
|||||
|