Here you can find the source of generateRandomString(String str, int length)
public static String generateRandomString(String str, int length)
//package com.java2s; //License from project: Apache License import java.util.Random; public class Main { private static final Random _RAND = new Random(System.currentTimeMillis()); private static final String _CHARTECER = "ABCDEFGHIJKLMNOPUVWXYZ0123456789"; public static String generateRandomString(String str, int length) { String encoder = null;/*from w ww . j a va 2 s .c o m*/ if (str == null || str.trim().length() == 0) { encoder = _CHARTECER; } else { encoder = str.trim(); } StringBuilder randomString = new StringBuilder(); int limit = encoder.length(); for (int index = 0; index < length; index++) { randomString.append(encoder.charAt(_RAND.nextInt(limit))); } return randomString.toString(); } }