I've been using RandomStringUtils
to generate random IDs to be used as database keys:
import org.apache.commons.lang.RandomStringUtils;
public class RandomStringTest {
public static void main(final String[] args) {
for (int i = 0; i <= 10; i++) {
final String id = RandomStringUtils.random(8,
"0123456789abcdefghijklmnopqrstuvwxyz");
System.out.println(id);
}
}
}
The key space is large enough,
len("0123456789abcdefghijklmnopqrstuvwxyz")^8 = 2821109907456 ≃ 10^12
Is the random mechanism properly seeded? I need to know that the keys are properly distributed before applying this to production.
By the way, the test code showed no repetitions when executed a few times, but that's far from solid proof.
There is no seed provided in the implementation I found, there is just a new Random()
. But there is a method where you can supply your own source of randomness
random(int count, int start, int end,
boolean letters, boolean numbers,
char[] chars, java.util.Random random)