import java.security.SecureRandom ;
import java.util.Random ;
public class Main {
private static final char [ ] CHARSET_aZ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray ( ) ;
private static final char [ ] CHARSET_AZ_09 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .toCharArray ( ) ;
private static final char [ ] CHARSET_HEX = "0123456789ABCDEF" .toCharArray ( ) ;
private static final char [ ] CHARSET_SPECIAL = { '!' , 'A' , 'B' } ;
public static String randomString
( char [ ] characterSet,
int length
) { char [ ] result = new char [ length] ;
for ( int i = 0 ; i < result.length ; i++ ) {
// picks a random index out of character set > random character
int randomCharIndex = random.nextInt ( characterSet.length ) ;
result[ i] = characterSet[ randomCharIndex] ;
}
}
public static void main
( String [ ] args
) { System .
out .
println ( "using a-z A-Z" ) ; for ( int i = 0 ; i < 5 ; i++ )
System .
out .
println ( randomString
( CHARSET_aZ,
30 ) ) ;
System .
out .
println ( "\n using A-Z 0-9" ) ; for ( int i = 0 ; i < 5 ; i++ )
System .
out .
println ( randomString
( CHARSET_AZ_09,
30 ) ) ;
System .
out .
println ( "\n using HEX" ) ; for ( int i = 0 ; i < 5 ; i++ )
System .
out .
println ( randomString
( CHARSET_HEX,
30 ) ) ;
System .
out .
println ( "\n using SPECIAL" ) ; for ( int i = 0 ; i < 5 ; i++ )
System .
out .
println ( randomString
( CHARSET_SPECIAL,
30 ) ) ;
System .
out .
println ( "\n increasing length" ) ; for ( int i = 0 ; i < 10 ; i++ )
System .
out .
println ( "len " + i
+ " => " + randomString
( CHARSET_AZ_09, i
) ) ; }
}
aW1wb3J0IGphdmEuc2VjdXJpdHkuU2VjdXJlUmFuZG9tOwppbXBvcnQgamF2YS51dGlsLlJhbmRvbTsKCnB1YmxpYyBjbGFzcyBNYWluIHsKCiAgICBwcml2YXRlIHN0YXRpYyBmaW5hbCBjaGFyW10gQ0hBUlNFVF9hWiA9ICJhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5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