How do I generate a random alphanumeric string?

The code below show you how to use the Apache Commons-Lang RandomStringUtils class to generate some random string data.

package org.kodejava.commons.lang;

import org.apache.commons.lang3.RandomStringUtils;

public class RandomStringUtilsDemo {
    public static void main(String[] args) {
        // Creates a 64-chars length random string of number.
        String result = RandomStringUtils.random(64, false, true);
        System.out.println("random = " + result);

        // Creates a 64-chars length of random alphabetic string.
        result = RandomStringUtils.randomAlphabetic(64);
        System.out.println("random = " + result);

        // Creates a 32-chars length of random ascii string.
        result = RandomStringUtils.randomAscii(32);
        System.out.println("random = " + result);

        // Creates a 32-chars length of string from the defined array of
        // characters including numeric and alphabetic characters.
        result = RandomStringUtils.random(32, 0, 20, true, true,
                "qw32rfHIJk9iQ8Ud7h0X".toCharArray());
        System.out.println("random = " + result);
    }
}

The example of our program result are:

random = 6251115933802303614285104650603664973302700879175809706257640062
random = EbKuzxERdBtCCHtbRuYnLwfnirtIqkcwwHxvZofkaGLNsNblCaVoLXFxfjYjXSHk
random = v9cJK?=d-Jl b-pisn9vUGxCV1&*>StY
random = XirkhUHHfwUf3kih8Hw877882d9i8Q3q

Maven Dependencies

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.13.0</version>
</dependency>

Maven Central

How do I generate a random array of numbers?

Using java.util.Random class we can create random data such as boolean, integer, floats, double. First you’ll need to create an instance of the Random class. This class has some next***() methods that can randomly create the data.

package org.kodejava.util;

import java.util.Arrays;
import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        Random r = new Random();

        // generate some random boolean values
        boolean[] booleans = new boolean[10];
        for (int i = 0; i < booleans.length; i++) {
            booleans[i] = r.nextBoolean();
        }
        System.out.println(Arrays.toString(booleans));

        // generate a uniformly distributed int random numbers
        int[] integers = new int[10];
        for (int i = 0; i < integers.length; i++) {
            integers[i] = r.nextInt();
        }
        System.out.println(Arrays.toString(integers));

        // generate a uniformly distributed float random numbers
        float[] floats = new float[10];
        for (int i = 0; i < floats.length; i++) {
            floats[i] = r.nextFloat();
        }
        System.out.println(Arrays.toString(floats));

        // generate a Gaussian normally distributed random numbers
        double[] doubles = new double[10];
        for (int i = 0; i < doubles.length; i++) {
            doubles[i] = r.nextGaussian();
        }
        System.out.println(Arrays.toString(doubles));
    }
}

The result of the code snippet above are:

[true, true, false, true, false, true, true, false, false, true]
[34195704, 1462972230, -475641915, -1531017612, 332184915, 1555901473, -276309016, 1433394157, -451221924, -1178823255]
[0.3040716, 0.28814012, 0.34028566, 0.021003246, 0.49158317, 0.37954164, 0.5403056, 0.54187375, 0.7157934, 0.5964742]
[1.051268591002577, -1.1283332046139989, 0.8351395528722437, -0.24598168031182968, 0.7123515366756693, -0.9661681996105319, -1.6107009669125059, 0.43994917963255387, 1.1309345726165914, 1.2321618489324313]

For an example to create random number using the Math.random() method see How do I create random number?.

How do I create random number?

The java.lang.Math.random() method returns random number between 0.0 and 1.0 including 0.0 but not including 1.0. By multiplying Math.random() result with a number, for example 10 will give us a range of random number between 0.0 and 10.0.

To get a random number between two numbers (n and m) we can use the formula of: n + (Math.random() * (m - n)). Where n is the lowest number (inclusive) and m is the highest number (exclusive).

package org.kodejava.lang;

public class RandomNumberExample {
    public static void main(String[] args) {
        // The Math.random() returns a random number between 0.0 and 1.0 
        // including 0.0 but not including 1.0.
        double number = Math.random();
        System.out.println("Generated number: " + number);

        // By multiplying Math.random() result with a number will give
        // us a range of random number between, for instance 0.0 to 10.0 as
        // shown in the example below.
        number = Math.random() * 10;
        System.out.println("Generated number: " + number);

        // To get a random number between n and m we can use the formula:
        // n + (Math.random() * (m - n)). The example below creates random
        // number between 100.0 and 200.0.
        int n = 100;
        int m = 200;
        number = n + (Math.random() * (m - n));
        System.out.println("Generated number: " + number);

        // Creates an integer random number
        int random = 100 + (int) (Math.random() * 100);
        System.out.println("Generated number: " + random);
    }
}

Here is an example result of our program.

Generated number: 0.024974902600698234
Generated number: 2.271051510086771
Generated number: 147.542543014888
Generated number: 112