Last active
October 29, 2016 08:31
-
-
Save maslick/030045af847fa416e21ff9d836609615 to your computer and use it in GitHub Desktop.
random string helper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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