【问题标题】:How to avoid generating duplicate numbers using RandomStringUtils.random of Apache Commons Jar如何使用 Apache Commons Jar 的 RandomStringUtils.random 避免生成重复数字
【发布时间】: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


【解决方案1】:

首先你有一个错误 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++;
        }

【讨论】:

  • 我尝试一个一个地生成它们,但是在一个点之后再次生成重复的数字
  • @ManishaSingh 他们是不是都是独一无二的?或者如果它们都需要唯一而不是仅仅删除 .clear 并且所有都应该工作,那么它们是否意味着在批次的基础上是唯一的
【解决方案2】:

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

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;
}

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

【讨论】:

  • 问题是 RandomStringUtils 正在生成重复的数字。如果您正在使用数组,那么该数组仍然可能有重复的数字,因此循环可能会无限。如果我可以避免重复生成,我需要明确的方法
  • 在运行上述代码后,您是否仍然在数组中得到重复的 Strings?不过这真的是不可能的。请至少运行代码,运行代码后在注释中显示示例数组??我以为我们在这里生成随机 Strings 而不是 int??
  • public static String randomNumeric(int count) 不生成随机数。它生成随机字符串。请确定你在这里问什么?请运行代码并提供一个看起来不合适的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多