Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Randomly generate distinct names

Tags:

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.

like image 860
well actually Avatar asked Feb 17 '11 06:02

well actually


People also ask

How do you generate random unique strings in Java?

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.

How do you generate random words in Java?

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.

How do you generate a random number without duplicates in Java?

int number=random. nextInt(int minbound,int maxbound); add the generated number into set because set doesn't allow duplicates value.


1 Answers

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

like image 184
Gaurav Lad Avatar answered Sep 30 '22 07:09

Gaurav Lad