Here you can find the source of getRandomString(int len)
Parameter | Description |
---|---|
len | a parameter |
public static String getRandomString(int len)
//package com.java2s; //License from project: Open Source License import java.util.Random; public class Main { private static final char[] consonants = "bcdfghjklmnpqrstvwxz".toCharArray(); /**// ww w . j av a 2s.c o m * Note Random is thread safe. Is using it across threads a bottleneck? If * you need repeatable results, you should create your own Random where you * can control both the seed and the usage. */ private static final Random r = new Random(); private static final char[] vowels = "aeiouy".toCharArray(); /** * A random lower case string * * @param len * @return */ public static String getRandomString(int len) { Random rnd = getRandom(); char[] s = new char[len]; for (int i = 0; i < len; i++) { // roughly consonant-consonant-vowel for pronounceability char c; if (rnd.nextInt(3) == 0) { c = vowels[rnd.nextInt(vowels.length)]; } else { c = consonants[rnd.nextInt(consonants.length)]; } s[i] = c; } return new String(s); } /** * @return a Random instance for generating random numbers. This is to avoid * generating new Random instances for each number as the results * aren't well distributed. * <p> * If you need repeatable results, you should create your own * Random. * <p> * Note: Java's Random <i>is</i> thread safe, and can be used by * many threads - although for high simultaneous usage, you may wish * to create your own Randoms. */ public static Random getRandom() { return r; } }