Skip to content

Instantly share code, notes, and snippets.

@maslick
Last active October 29, 2016 08:31
Show Gist options
  • Save maslick/030045af847fa416e21ff9d836609615 to your computer and use it in GitHub Desktop.
Save maslick/030045af847fa416e21ff9d836609615 to your computer and use it in GitHub Desktop.
random string helper class
import java.util.Random;
public final class Helper {
public static int randomInteger(int min, int max) {
return min + (int) (Math.random() * ((max - min) + 1));
}
public static String randomString(int length) {
String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String numbers = "0123456789";
Random r = new Random();
String alphabet = letters + numbers;
String out = "";
for (int i = 0; i < length; i++) {
out += alphabet.charAt(r.nextInt(alphabet.length()));
}
return out;
}
public static String randomString() {
return randomString(5);
}
public static void main(String[] args) {
System.out.println(randomString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment