public static String getRandomString ( int numberOfLetters , char [ ] alphabet , boolean includeNumbers ) { StringBuilder builder = new StringBuilder ( ) ; int upperRange = alphabet . length - 1 ; // make sure first character is not a number builder . append ( alphabet [ generator . nextInt ( upperRange ) ] ) ; if ( includeNumbers ) { upperRange += NUMBERS . length ; } for ( int i = 1 ; i < numberOfLetters ; i ++ ) { int letterIndex = generator . nextInt ( upperRange ) ; if ( letterIndex > alphabet . length - 1 ) { builder . append ( NUMBERS [ letterIndex - alphabet . length ] ) ; } else { builder . append ( alphabet [ letterIndex ] ) ; } } return builder . toString ( ) ; }
Static random number generator aware string generating method .