previous | start | next

Random Class

Instead of manually creating a text file of integers for testing, the Random class has a method to create pseudo random sequences of integers or doubles and the other numeric types. It also lets you restrict the range of the numbers generated; e.g. you may only want values in the range 0 to 100 in one case or 2 to 12 in another case (two dice).

        Random r = new Random();
        int x;

        x = r.nextInt(101); // x will be a random int with 0 <= x < 101
        y = r.nextInt(11);  // y will be a random int with 0 <= y < 10
     


previous | start | next