Java
Chilkat2 Java
Generating Random ASCII Strings
See more PRNG Examples
Demonstrates how to generate random us-ascii strings.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.Prng;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
success = false;
Prng fortuna = new Prng();
// Generate random strings having only lowercase chars (a-z)
// Disallow digits and uppercase and only allow lowercase
boolean bDigits = false;
boolean bUppercase = false;
boolean bLowercase = true;
System.out.println("-- only lowercase alpha (a-z)");
for (int i = 1; i <= 10; i++) {
// Generate 20-character strings.
System.out.println(fortuna.randomString(20, bDigits, bLowercase, bUppercase));
}
// Allow both lowercase and uppercase alpha chars
bUppercase = true;
System.out.println("-- lower and uppercase alpha (a-zA-Z)");
for (int i = 1; i <= 10; i++) {
// Generate 20-character strings.
System.out.println(fortuna.randomString(20, bDigits, bLowercase, bUppercase));
}
// Allow digits (0-9)
bDigits = true;
System.out.println("-- digits and lower/uppercase alpha (0-9a-zA-Z)");
for (int i = 1; i <= 10; i++) {
// Generate 20-character strings.
System.out.println(fortuna.randomString(20, bDigits, bLowercase, bUppercase));
}
// Allow only digits (0-9)
bUppercase = false;
bLowercase = false;
System.out.println("-- only digits (0-9)");
for (int i = 1; i <= 10; i++) {
// Generate 20-character strings.
System.out.println(fortuna.randomString(20, bDigits, bLowercase, bUppercase));
}
}
}