I'm generating words randomly from several arrays of strings.
There are 4 arrays of lengths 7, 9, 15 and 24.
This means there are 22,680 possible permutations. To that I append a number of 0-999 to the end of the string, meaning I multiply the possible permutation by 999 for a total of over 22 million combination.
I'm using a list to evaluate the frequency of collisions and am getting some very low numbers. I never make it over 10,000 iterations before a duplicate string is found.
String[][] dictionary = // array of 4 string arrays
public String assemble() {
StringBuffer sb = new StringBuffer(30);
for(int i=0; i<4; i++) {
// for each strArr, select a random element
sb.append(select(dictionary[i]));
}
// add a number from 0 - 999 to the end of the string
sb.append(randInt(999));
return sb.toString();
}
private String select(String[] strArr) {
return strArr[randInt(strArr.length)];
}
List<String> generatedStrings = new ArrayList<>();
List<Integer> iterations = new ArrayList<>();
// iterate until the same string is generated twice or we make 1M strings
for (int i = 0; i < 1000000; i++) {
String s = assemble();
if (generatedStrings.contains(s)) {
iterations.add(i);
generatedStrings.clear();
break;
}
generatedStrings.add(s);
}
The method for generating the random integers:
SecureRandom secureRandom = new SecureRandom();
public int randInt(int length) {
return secureRandom.nextInt(length);
// also tried the following, though I think the underlying tech is the same:
// return (int) (Math.random() * length);
}
When I run this in a main method I'm getting numbers around 13k which seems very low. Is this an issue with the way I am generating the random numbers?
Aucun commentaire:
Enregistrer un commentaire