2017-07-05 14 views
0

Apache Commons Lang3パッケージクラスRandomStringUtilsを使用しています。いくつかの数字を生成した後、RandomStringUtils.randomNumericは無限ループで重複した数字を生成しています。これを防ぐ方法を教えてください。ここでApache Commons JarのRandomStringUtils.randomを使用して重複数が生成されないようにする方法

は私のコードです:

まず
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++; 
    } 
} 
+0

私はなぜあなたが勝ったのか驚いています;あなたの最初の実行ではArrayIndexOutOfBoundsExceptionがありますループここで、** numGen [idx - 1] = random **、** idx = 0 ** whileループの直前? – ShayHaned

+0

こんにちは@ShayHaned。私は –

答えて

0

バグnumGen [IDX - 1] =持ってランダムに。

最初にループに入るとき、idxは0なので、numGenの-1フィールドに割り当てます。 第2に、それは永遠に続くことはありませんが、バッチ全体を削除するのではなく、文字列を生成するときに長い間別のアプローチをとることができます。

このバッチで番号がまだ生成されていない場合は、各世代の後に確認してください。あなたは、Javaマップを使用することができますまたはルックアップのバッチ内のすべての見た目の数字を高速にマークするように設定します。

だから、それらの線に沿って何かを見て、あなたのソリューションのためのコードを追加します。

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

を更新しました。私はそれらを1つずつ生成しようとしましたが、1つのポイントの後に再び重複する数字が生成されます。 –

+0

@ManishaSinghはすべてユニークであることを意味していますか?または、それらが一意である必要がある場合、バッチ単位でユニークであることを意味します。.clearをすべて削除してください。 – cerkiewny

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 []型の配列、文字列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; 
} 

このサンプルを実行しても問題が解決しない場合は、問題を再確認することができます:)

+0

RandomStringUtilsは重複した番号を生成していましたか?あなたが配列を取っているなら、配列は重複した数を持つかもしれないので、ループは無限に進むかもしれません。重複した生成を避けることができるのであれば、明確なアプローチが必要です –

+0

上記のコードを実行した後も、配列内に重複する**文字列**が残っていますか?しかし、それは本当に不可能です。コードを少なくとも実行して、コードを実行した後でコメントのサンプル配列を表示してください。私は** int **の代わりにランダム** String **を生成していると思っていましたか? – ShayHaned

+0

** public static String randomNumeric(int count)**乱数を生成しません。ランダムなStringを生成します。あなたがここで何を求めているかを確認してください?コードを実行し、不適切と思われるサンプルを提示してください – ShayHaned

関連する問題