You need random string generator. This answer I stole from here.
protected String getSaltString() {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 10) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
return salt.toString();
}
Call it as getSaltString()+"@gmail.com" in you code
You need random string generator. This answer I stole from here.
protected String getSaltString() {
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 10) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
return salt.toString();
}
Call it as getSaltString()+"@gmail.com" in you code
You can create a method for generating the unique id
public static String getUniqueId() {
return String.format("%s_%s", UUID.randomUUID().toString().substring(0, 5), System.currentTimeMillis() / 1000);
}
And then use this method with the hostname which you need
public static String generateRandomEmail() {
return String.format("%s@%s", getUniqueId(), "yourHostName.com");
}
Another solution:
Add dependency for javafaker.Faker https://github.com/DiUS/java-faker
import com.github.javafaker.Faker;
public static String randomEmail() {
Faker faker = new Faker();
return faker.internet().emailAddress();
}
The problem with your code is that each call of faker.name().fullName() will generate a new name. (Also faker.name().username() has no relation to any previously generated full name.)
You could change your code to this:
(Assuming that each fullName consist of at least two parts, seperated by a space)
String fullName = faker.name().fullName();
String firstName = fullName.substring(fullName.indexOf(' '));
p.setEmail(firstName + "@lucatinder.org");
p.setAge(faker.number().numberBetween(18, 90));
p.setGender(sex);
p.setName(fullName);
p.setPassword(faker.lorem().characters(8, 16));
However that won't solve your gender issue, as that does not seem to be a supported feature.
Check this related issue on the github project.
For whatever you're trying to do with that generated data, there (normally) shouldn't be an issue if the sex does not match to the name.
According to the Faker documentation, it appears you are getting a different name each time you invoke .name().
Each call to method fake.name() yields a different (random) result. This is because faker forwards faker.Generator.method_name() calls to faker.Generator.format(method_name).