How do I generate random string?


import java.util.Random;

public class RandomString {
    public static final String SOURCES =

    public static void main(String[] args) {
        RandomString rs = new RandomString();
        System.out.println(rs.generateString(new Random(), SOURCES, 10));
        System.out.println(rs.generateString(new Random(), SOURCES, 10));
        System.out.println(rs.generateString(new SecureRandom(), SOURCES, 15));
        System.out.println(rs.generateString(new SecureRandom(), SOURCES, 15));

     * Generate a random string.
     * @param random the random number generator.
     * @param characters the characters for generating string.
     * @param length the length of the generated string.
     * @return
    public String generateString(Random random, String characters, int length) {
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = characters.charAt(random.nextInt(characters.length()));
        return new String(text);

Example string produced by the code snippets are:


How to get random key-value pair from Hashtable?

package org.kodejava.example.util;

import java.util.Hashtable;
import java.util.Random;

public class HashtableGetRandom {
    public static void main(String[] args) {
        // Create a hashtable and put some key-value pair.
        Hashtable<String, String> colors = new Hashtable<>();
        colors.put("black", "#000");
        colors.put("red", "#f00");
        colors.put("green", "#0f0");
        colors.put("blue", "#00f");
        colors.put("white", "#fff");

        // Get a random entry from the hashtable.
        String[] keys = colors.keySet().toArray(new String[colors.size()]);
        String key = keys[new Random().nextInt(keys.length)];
        System.out.println(key + " = " + colors.get(key));

How do I pick a random value from an enum?

The following code snippet will show you how to pick a random value from an enum. First we’ll create an enum called BaseColor which will have three valid value. These values are Red, Green and Blue.

To allow us to get random value of this BaseColor enum we define a getRandomColor() method in the enum. This method use the java.util.Random to create a random value. This random value then will be used to pick a random value from the enum.

Let’s see the code snippet below:

package org.kodejava.example.basic;

import java.util.Random;

public class EnumGetRandomValueExample {
    public static void main(String[] args) {
        // Pick a random BaseColor for 10 times.
        for (int i = 0; i < 10; i++) {
            System.out.printf("color[%d] = %s%n", i, 

     * BaseColor enum.
    private enum BaseColor {

         * Pick a random value of the BaseColor enum.
         * @return a random BaseColor.
        public static BaseColor getRandomColor() {
            Random random = new Random();
            return values()[random.nextInt(values().length)];

The output of the code snippet:

color[0] = Blue
color[1] = Red
color[2] = Red
color[3] = Green
color[4] = Blue
color[5] = Blue
color[6] = Green
color[7] = Red
color[8] = Red
color[9] = Green