Java Random Generator nextInt Examples

Java 8 Generate random integers with nextInt from 0 to 100

To generate a series of random integers, you need to use a Random object. One object Random is enough to generate many numbers. Below you can find example of generating 1000 integers in interval from 0 to 100:

package number;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Randoma {

    public static final void main(String... aArgs) {
        //using single Random
        Random randomGenerator = new Random();

        ArrayList<Integer> items = new ArrayList<Integer>(1000);

        //generating 1000 random numbers from 1 to 100
        for (int idx = 1; idx <= 1000; ++idx) {
            int randomInt = randomGenerator.nextInt(100);
            System.out.println(randomInt);
            items.add(randomInt);
        }
        //test randomness and print the result
        count(items);
    }
    
    // group count the list items and print them
    private static void count(List<Integer> items) {
        Map<Integer, Long> result =
                items.stream().collect(
                        Collectors.groupingBy(
                                Function.identity(), Collectors.counting()
                        )
                );
        //print the number and how much times is generated
        result.forEach((item, value) -> System.out.println(item + " - " + value));
    }
}

In the above code we are generating 1000 random numbers. We see that:

int randomInt = randomGenerator.nextInt(100);

generates some random numbers which satisfy the general case. In next section we will test the randomness.

Java 8 Generate random integers in range

Generating random numbers in range can be done by nextInt as well. The example below shows how to generate random numbers from 20 to 100:

package number;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Randoma {

    public static final void main(String... aArgs) {

        ArrayList<Integer> itemsR = new ArrayList<Integer>(1000);

        //generating 1000 random numbers from 20 to 100
        for (int idx = 1; idx <= 1000; ++idx) {
            int randomInt = randomRange(20, 100);
            System.out.println(randomInt);
            itemsR.add(randomInt);
        }
        //test randomness and print the result
        count(itemsR);
    }

    // group count the list items and print them
    private static void count(List<Integer> items) {
        Map<Integer, Long> result =
                items.stream().collect(
                        Collectors.groupingBy(
                                Function.identity(), Collectors.counting()
                        )
                );
        //System.out.println(result);
        //result.forEach((item,value)->System.out.println("number: " + item + " ís found: " + value));
        result.forEach((item, value) -> System.out.println(item + " - " + value));
    }

    //generate random numbers in range
    private static int randomRange(int min, int max) {

        if (min >= max) {
            throw new IllegalArgumentException("Min parameter can't be bigger than Max");
        }

        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }
}

Java 8 test nextInt randomness

Testing randomness is done by method count:

    private static void count(List<Integer> items) {
        Map<Integer, Long> result =
                items.stream().collect(
                        Collectors.groupingBy(
                                Function.identity(), Collectors.counting()
                        )
                );
        //print the number and how much times is generated
        result.forEach((item, value) -> System.out.println(item + " - " + value));
    }

It do group count of the element list and print them in the console. After several executions this is the average result:

As we can see the spread of the generated numbers is equal. Which guarantee that this method satisfy most daily problems related to random integers in java.