Here you can find the source of randomString(Integer len)
Parameter | Description |
---|---|
len | length of string to generate |
public static String randomString(Integer len)
//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); } }