Here you can find the source of randomStr(int length, String charSet)
Parameter | Description |
---|---|
length | a parameter |
charSet | character set |
public static String randomStr(int length, String charSet)
//package com.java2s; //License from project: Apache License public class Main { /**//from w w w.j a v a 2s. c om * Generates random alphanumeric String of the specified length using characters specified in charSet. * * @param length * @param charSet character set */ public static String randomStr(int length, String charSet) { char[] result = new char[length]; fillWithRandomChars(result, charSet); return new String(result); } /** * Populates given char array with random string using characters from * specified character set. Can be used for memory conservation purposes. * * @param str * @param charSet */ public static void fillWithRandomChars(char[] str, String charSet) { char[] ALPHA_NUM = charSet.toCharArray(); for (int i = 0; i < str.length; i++) { str[i] = ALPHA_NUM[(int) (Math.random() * ALPHA_NUM.length)]; } } }