mercredi 5 juillet 2017

How to avoid generating duplicate numbers using RandomStringUtils.random of Apache Commons Jar

I am using Apache Commons Lang3 package class RandomStringUtils. After generating some numbers, RandomStringUtils.randomNumeric is generating duplicate numbers in an infinite loop. How can I prevent this from happening?

Here is my code:

quantity = 100000
insertedNum = 0;
length = 9;
String[] numGen = new String[100];
idx = 0;

while (insertedNum < quantity) {
    String random=RandomStringUtils.randomNumeric(length);
    numGen[idx - 1] = random;
    if (idx == 100) {
        insertedNum += DB Code. If unique constraint error then discard batch return 0 else execute batch return inserted count.
        idx = 1;
        numGen = new String[100];
    }
    else
        idx++;
    }
}




Aucun commentaire:

Enregistrer un commentaire