Skip to content

Instantly share code, notes, and snippets.

@rynr
Last active August 19, 2016 08:45
Show Gist options
  • Save rynr/eddae60085eace987c3318a3d9437e83 to your computer and use it in GitHub Desktop.
Save rynr/eddae60085eace987c3318a3d9437e83 to your computer and use it in GitHub Desktop.
TestHelper.java
package org.rjung.tools.testing;
import java.math.BigInteger;
import java.security.SecureRandom;
public class TestHelper {
private static final int DEFAULT_STRING_LENGTH = 8;
private static final int DEFAULT_MAX_INT = 255;
private static SecureRandom random;
public static String getRandomString() {
return getRandomString(DEFAULT_STRING_LENGTH);
}
public static String getRandomString(int length) {
return new BigInteger(length * 5, getRandom()).toString(32);
}
public static int getRandomInt() {
return getRandomInt(DEFAULT_MAX_INT);
}
public static int getRandomInt(int max) {
return getRandomInt(0, max);
}
public static int getRandomInt(int min, int max) {
return getRandom().nextInt((max - min) + 1) + min;
}
private static SecureRandom getRandom() {
if (random == null) {
random = new SecureRandom();
}
return random;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment