1

I have generated a User class which includes the first name and the last name. I wan to ask how can I generate different random names with using random generator?

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class User {
private String firstName;
private String lastName;
private int age;

/**
 * @return the firstName
 */
public String getFirstName() {
    return firstName;
}
/**
 * @param firstName the firstName to set
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
/**
 * @return the lastName
 */
public String getLastName() {
    return lastName;
}
/**
 * @param lastName the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}
/**
 * @return the age
 */
public int getAge() {
    return age;
}
/**
 * @param age the age to set
 */
public void setAge(int age) {
    this.age = age;
}


}

Can anyone show me some snippet code on how to deal with this generator? Please teach me. Thank you.

Sofo Gial
  • 697
  • 1
  • 9
  • 20
BingHao
  • 31
  • 1
  • 6
  • Do check random related functions from java and try to implement, once donem and if you get errors, you could come back with an actual problem we could help you with. As it is written this question is too broad. – Cleptus Nov 21 '18 at 08:44

4 Answers4

2

You can use Collections.shuffle

List<String> fName = Arrays.asList("Jim", "Fred", "Baz", "Bing");
Collections.shuffle(fName );
List<String> lName = Arrays.asList("Duck", "Swan", "Cooper", "Bing");
Collections.shuffle(lName );
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

While standard library doesn't contains methods for generating random strings you can use org.apache.commons.lang3 it contains RandomStringUtils.randomAlphanumeric and some other helpful methods.

talex
  • 17,973
  • 3
  • 29
  • 66
0

You can use something like this if you want to generate Strings,

import java.util.HashSet;
import java.util.Set;

public class Main {

    final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    final java.util.Random rand = new java.util.Random();

    final Set<String> identifiers = new HashSet<>();

    public String getName() {
        StringBuilder builder = new StringBuilder();
        while (builder.toString().length() == 0) {
            int length = rand.nextInt(5) + 5;
            for (int i = 0; i < length; i++) {
                builder.append(letters.charAt(rand.nextInt(letters.length())));
            }
            if (identifiers.contains(builder.toString())) {
                builder = new StringBuilder();
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        Main a = new Main();
        for (int i = 0; i < 10; i++) {
            System.out.println(a.getName());
        }
    }
}

Taken it from here

Sandeepa
  • 3,457
  • 5
  • 25
  • 41
0

The below code generates 100 strings of length 10

public static void main(String[] args) {
    int leftLimit = 97; // letter 'a'
    int rightLimit = 122; // letter 'z'
    int targetStringLength = 10;
    Random random = new Random();

    for(int x=1;x<=100;x++) {
        StringBuilder buffer = new StringBuilder(targetStringLength);
    for (int i = 0; i < targetStringLength; i++) {
        int randomLimitedInt = leftLimit + random.nextInt(rightLimit - leftLimit + 1);
        buffer.append((char) randomLimitedInt);
    }
    String generatedString = buffer.toString();


    System.out.println(generatedString);
    }
}


ucrahhupoy
wbvvihsvcf
inasxtyekv
hnyvuehwag
cntymnfthh
jfwqskssrj
chmqciriry
nicdlipttl
bglhilhqbp
uwatjqqymc
tdjfcatmqg
eflgaiiqlo
seyloflwtp
ynbspwpgem
ayiwbybrgj
drdxkxplrt
hwmnuhgdus
ouvurdqpgb
homcnsqjmp
usnmevzfne
pgnjrxuplh
xnorrahdfy
pgkmtfosrn
jnilgrpvel
sqmxwsfvsl
trzuqlvtcz
ylnlupikca
wlydkuzpuf
xxxjnkpjqd
kucqqrzipq
atfywxtlsc
xkyoyjhprn
fmpemwbncl
vdxzhsvecd
swbnmtwefc
jvaxxqnpsn
jldyxsqyca
kjzafkgbhh
fhvlzsnwnt
ogxcwxglsh
qbvkxakxfk
dthenridxv
nodssypxjz
pxiiqgewlb
oiktptxvjc
ejbsrdnrtr
lefghacxhv
ovljnhxwpv
jiajtejzsj
qmrndmklpz
vdkniqkliv
qptyowbqhm
dythyetfnt
prhtwsload
qywfvdxbuu
frfbrzfaxv
qinwrqhama
tjjexrylnt
osgmqmhvjf
akfkcbyclj
phiqvvvxus
nnjlmrmzhe
vuwznjhuva
aysjsiaalt
hfldzsesag
iieoorivom
qxruumxzbi
srqjzfcqbl
qwmphgytmz
zomrsdxvno
skihgctowr
kvvbwqazzw
ylmsomofvf
xotjnoeiha
aqwjejizgp
zlizdurnjv
uesltpfyza
hzvsiocmss
wytpnqkxvd
vllgqkgbey
zdmpqvlizr
eganvnkbou
joxeadhuiw
ozsiidmzaz
bhovdeuvod
xmqvveeifw
cxrnaphnip
npkgljejet
mjgptdgshv
xbbfzgjkgz
xfxvumjlzh
siihmppnda
zbjsiobxpz
lkqbzwsynr
tppanksrww
dqvvmbmtgb
uehlkcxhke
xagyduvldq
plxkorrdkj
bhghwbhncr

Using RandomStringUtils

  public static void main(String[] args) {



        int length = 10;
        boolean useLetters = true;
        boolean useNumbers = false;
        for(int x=1;x<=10;x++) {
        String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
        System.out.println(generatedString);
        }


    }



uSkhYvaHTB
rkJiZUHSuV
gNgePMJRRh
zYzOEHroOh
RtSLGvwwjn
zNeogkmdtT
lkPFDPYOrE
SYbuurRWoV
fOYEACrTzt
SrIABcTyLM

Hope it helps.

Shiva
  • 1,962
  • 2
  • 13
  • 31