Java Random String randomString(Integer len)

Here you can find the source of randomString(Integer len)

Description

Generates random alpha-numeric string of specified length

License

Open Source License

Parameter

Parameter Description
len length of string to generate

Return

random string (A-Z, 0-9)

Declaration

public static String randomString(Integer len) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//  w w  w  .  ja va  2s  .c om
     * Generates random alpha-numeric string of specified length
     *
     * @param len length of string to generate
     * @return random string (A-Z, 0-9)
     */
    public static String randomString(Integer len) {
        final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rnd = new Random();

        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
            sb.append(AB.charAt(rnd.nextInt(AB.length())));
        return sb.toString();
    }

    /**
     * Generates random alpha-numeric string with default length of 16
     *
     * @return random alpha-numeric string with default length of 16
     */
    public static String randomString() {
        return randomString(16);
    }

    /**
     * Chooses random string from those provided in list
     *
     * @param diffValues list of string to select from
     * @return random value from list
     */
    public static String randomString(List<String> diffValues) {
        int select = new Random().nextInt(diffValues.size());
        return diffValues.get(select);
    }
}

Related

  1. randomString(int length)
  2. randomString(int length)
  3. randomString(int length)
  4. randomstring(int lo, int hi)
  5. randomString(int stringLength)
  6. randomString(Random r, int minCount, int maxCount)
  7. randomSubset(String[] list, int count)
  8. randomSuffix(final String name)
  9. randomUnicodeString(Random r)