I need to generate 10,000 unique identifiers in Java. The identifiers should be a mixture of numbers and letters and less than 10 characters each. Any ideas? Built in libraries would be an extra plus.
Using randomUUID() util. UUID is another Java class that can be used to generate a random string. It offers a static randomUUID() method that returns a random alphanumeric string of 32 characters.
Example 1: Java program to generate a random string Next, we have generated a random index number using the nextInt() method of the Random class. Using the random index number, we have generated the random character from the string alphabet. We then used the StringBuilder class to append all the characters together.
int number=random. nextInt(int minbound,int maxbound); add the generated number into set because set doesn't allow duplicates value.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With