Here you can find the source of generateString(final String letters, int length)
Parameter | Description |
---|---|
letters | string containing symbols for new string |
length | length of the result string |
public static String generateString(final String letters, int length)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { public static final Random r = new Random(); /**//from w w w .j a v a 2 s . co m * Generates quasi-random String of given length. * To generate passwords use SecurityHelper class. * * @param letters string containing symbols for new string * @param length length of the result string * @return generated string */ public static String generateString(final String letters, int length) { StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { sb.append(letters.charAt(r.nextInt(letters.length()))); } return sb.toString(); } public static String generateString(int length, final String... letters) { StringBuilder sb = new StringBuilder(); while (length-- > 0) { sb.append(letters[r.nextInt(letters.length)]); } return sb.toString(); } }