Java: Randomly generate distinct names
I need to generate 10,000 unique identifiers in Java. The ide开发者_如何学JAVAntifiers should be a mixture of numbers and letters and less than 10 characters each. Any ideas? Built in libraries would be an extra plus.
I am answering this very late, but this is what really useful for new reader. This is a very simple and efficient way to get random VALID names. To do so, add maven repository in POM.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.12</version>
</dependency>
And then use the Faker class as below in your Java code
Faker faker = new Faker();
String name = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
String streetAddress = faker.address().streetAddress();
Try printing the result using standard System.out.println();
For more reference Faker Lib
// class variable
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
final java.util.Random rand = new java.util.Random();
// consider using a Map<String,Boolean> to say whether the identifier is being used or not
final Set<String> identifiers = new HashSet<String>();
public String randomIdentifier() {
StringBuilder builder = new StringBuilder();
while(builder.toString().length() == 0) {
int length = rand.nextInt(5)+5;
for(int i = 0; i < length; i++) {
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
}
if(identifiers.contains(builder.toString())) {
builder = new StringBuilder();
}
}
return builder.toString();
}
Why not use java.util.UUID
?
It is guaranteed to generate unique identifiers, and it is as standard as it gets :-).
e.g.
String random = UUID.randomUUID().toString();
Or even
int desiredLength = 5;
String random = UUID.randomUUID()
.toString()
.substring(0, desiredLength);
Which will result in some random String
of desiredLength
, like:
6e9c3
If you permit Apache Commons lang...
public String[] getRandomlyNames(final int characterLength, final int generateSize) {
HashSet<String> list = new HashSet<String>();
for (int i = 0; i < generateSize; ++i) {
String name = null;
do {
name = org.apache.commons.lang.RandomStringUtils.randomAlphanumeric(
org.apache.commons.lang.math.RandomUtils.nextInt(characterLength - 1) + 1);
while(list.contains(name));
list.add(name);
}
return list.toArray(new String[]{});
}
I had the same problem, but I needed an arbitrarily long string. I came up with this one-liner, no external library needed, that will give you 10 characters:
BigInteger.probablePrime(50, new Random()).toString(Character.MAX_RADIX)
The length can be changed, you need about 5 bits per character. What did is filter and limit the length as follows (just lowercase letters, and size 10):
BigInteger.probablePrime(100, new Random()).
toString(Character.MAX_RADIX).
replaceAll("[0-9]", "").
substring(0, 10)
Disadvantage: it's a bit slow.
You can try to take md5 hash of current time and you will get "random" identifier as mixture of numbers and letters
The easiest and fastest way is to generate permutations of a certain string. As long as the string is long enough, you can easily have 10,000 unique permutations. The good thing of generating permutation is that you don't have to worry about duplications. If a string contains all different characters, it can generate n! permutations (n is the length of the string). So a string with 8 different characters can generate 40,320 different permutations.
There are many code on-line to generate permutations of a string, such as this one http://introcs.cs.princeton.edu/23recursion/Permutations.java.html.
If you want them to be more random, you can use different strings as the seed, such as "abcde123", "efgh456", etc..
You could try
Random rand = new Random();
Set<String> words = new HashSet<String>();
while(words.size() < 10000)
words.add(Long.toString(Math.abs(rand.nextLong() % 3656158440062976L), 36)));
The long constant is just enough for 10 digit, base 36 numbers.
精彩评论