0

我正在使用 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++;
    }
}
4

2 回答 2

0

一种麻烦的方法是不断重新生成随机字符串,直到您发现您遇到了一个尚未包含在数组中的唯一随机字符串。您所要做的就是检查新生成的随机字符串是否已经在数组中

quantity = 100000
insertedNum = 0;
length = 9;
String[] numGen = new String[100];
idx = 0;
String random = "";
while (insertedNum < quantity) {
            random=RandomStringUtils.randomNumeric(length);
            while( isAlreadyInArray( numGen , random ) )
            {
                //regenerate a random string
                random = RandomStringUtils.randomNumeric( length );
            }
            // Add it to your array, and increment it by one
            numGen[idx] = random;
            idx = (idx + 1) % numGen.length;
            // In this case, the array got populated to completion
            if (idx == 0) {
                System.out.println( java.util.Arrays.toString( numGen ) );  
                //clear the batch set
                insertedNum += DB Code. If unique constraint error then discard batch return 0 else execute batch return inserted count.
                numGen = new String[100];
            }
        }

这是辅助方法isAlreadyInArray( String[] array , String someVal )

public boolean isAlreadyInArray( String[] mData , String strToCheck )
{
    for( int x = 0; x < mData.length; x++ )
    {
        if( mData[x] != null && mData[x].equalsIgnoreCase( strToCheck ) )
        {
            return true;    
        }
    }
    return false;
}

请运行这个示例,如果它没有帮助,那么我们当然可以重新迭代这个问题:)

于 2017-07-05T08:45:54.433 回答
0

首先你有一个错误 numGen[idx - 1] = random;

首次进入循环时,idx 为 0,因此您分配给 numGen 中的 -1 字段。第二,它不会永远持续下去,但如果它持续很长时间,你可以在生成字符串时采取不同的方法,而不是删除整个批次,如果它们存在于当前批次中,则一个一个地重新生成它们。

如果该批次中尚未生成该编号,请尝试在每次生成后检查。您可以使用 java map 或 set 标记批处理中所有看到的数字,以便查找更快。

因此,为您的解决方案添加代码以查看以下内容:

quantity = 100000
insertedNum = 0;
length = 9;
String[] numGen = new String[100];
idx = 0;
Set<string> seenThisBatch = new HashSet<string>();
while (insertedNum < quantity) {
            String random=RandomStringUtils.randomNumeric(length);
            //in case of duplicates, keep on generating
            while(seenThisBatch.contains(random){
                  random=RandomStringUtils.randomNumeric(length);
            }
            //add it to set
            seenThisBatch.add(random);
            numGen[idx - 1] = random;
            if (idx == 100) {
                //clear the batch set
                seenThisBatch.clear();
                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++;
        }
于 2017-07-05T07:39:53.767 回答