【发布时间】:2017-12-08 17:27:57
【问题描述】:
我正在使用 Apache Commons Lang3 包类 RandomStringUtils。在生成一些数字后,RandomStringUtils.randomNumeric 正在无限循环中生成重复数字。我怎样才能防止这种情况发生?
这是我的代码:
quantity = 100000
insertedNum = 0;
length = 9;
String[] numGen = new String[100];
idx = 1;
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++;
}
}
【问题讨论】:
-
我很惊讶为什么您不会在循环的第一次运行中出现 ArrayIndexOutOfBoundsException ,其中 numGen[idx - 1] = random ,因为 idx = 0 就在 while 循环之前??
-
嗨@ShayHaned。我已经更新了
标签: java random apache-commons-lang3