8

数字の繰り返しのないランダムな4桁の数字を作成するためにJavaを使用してコードを書きました。私が書いたコードは以下のとおりです:-

Random r = new Random();
d1 = r.nextInt(9);
d2 = r.nextInt(9);
d3 = r.nextInt(9);
d4 = r.nextInt(9);
while(d1==d2||d1==d3||d1==d4||d2==d3||d2==d4||d3==d4)
{
    if(d1==d2||d2==d3||d2==d4)
    {
        d2 = r.nextInt(9);
    }
    if(d1==d3||d2==d3||d3==d4)
    {
        d3 = r.nextInt(9);
    }
    if(d1==d4||d2==d4||d3==d4)
    {
        d4 = r.nextInt(9);
    }
}   
System.out.println(d1+""+d2+""+d3+""+d4);


テストケース(から生成されたものSystem.out.println(R1+""+R2+""+R3+""+R4);)は次のとおりです:-

 0123 |  OK as required
 1234 |  OK as required
 2123 |  not OK because 2 is present more than one time 
 9870 |  OK as required
 0444 |  not OK because 4 is present more than one time


ここでの私の質問は、これを行うためのより良い方法があればということです。何らかの方法でそれを強化できたら?

4

7 に答える 7

18

0 から 9 までの整数のリストを作成し、それをシャッフルして最初の 4 つを抽出します。

public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<>();
    for(int i = 0; i < 10; i++){
        numbers.add(i);
    }

    Collections.shuffle(numbers);

    String result = "";
    for(int i = 0; i < 4; i++){
        result += numbers.get(i).toString();
    }
    System.out.println(result);
}

醜い文字列から整数への会話が行われていますが、その考えは理解できます。ユースケースに応じて、何が必要かがわかります。

于 2013-08-19T15:18:57.857 に答える
5

いくつかのアプローチ:

  1. セットを使用して数字を保持し、セットに 4 つの値が含まれるまでランダムな数字を追加し続けます。

  2. 値が 0 ~ 9 の配列を作成します。配列をシャッフルし、最初の 4 つの値を取得します。

パフォーマンスが重要な場合は、いくつかの異なる方法を試して、どちらが速いかを確認してください。

于 2013-08-19T15:20:42.597 に答える
4

多くの文字列解析を使用しますが、データ構造は使用しませんが、これが私のアプローチです。

 static int generateNumber(int length){
            String result = "";
            int random;
            while(true){
                random  = (int) ((Math.random() * (10 )));
                if(result.length() == 0 && random == 0){//when parsed this insures that the number doesn't start with 0
                    random+=1;
                    result+=random;
                }
                else if(!result.contains(Integer.toString(random))){//if my result doesn't contain the new generated digit then I add it to the result
                    result+=Integer.toString(random);
                }
                if(result.length()>=length){//when i reach the number of digits desired i break out of the loop and return the final result
                    break;
                }
            }

            return Integer.parseInt(result);
        }
于 2013-10-08T22:33:57.470 に答える
2

たぶんセットを使用しますか?

Random r = new Random();
Set<Integer> s = new HashSet<Integer>();
while (s.size() < 4) {
    s.add(r.nextInt(9));
}
于 2013-08-19T15:20:14.740 に答える
1

大まかに(テストされていません):

int randomNum = r.nextInt(5040);
int firstDigit = randomNum % 10;
randomNum = randomNum / 10;
int secondDigit = randomNum % 9;
randomNum = randomNum / 9;
int thirdDigit = randomNum % 8;
randomNum = randomNum / 8;
int fourthDigit = randomNum % 7;

if (secondDigit == firstDigit) {
  secondDigit++;
}

while ((thirdDigit == firstDigit) || (thirdDigit == secondDigit)) {
  thirdDigit++:
}

while ((fourthDigit == firstDigit) || (fourthDigit == secondDigit) || (fourthDigit == thirdDigit)) {
  fourthDigit++;
}

(これをコーディングした後、インクリメント操作はモジュロ 10 で行う必要があることに気付きました。)

于 2013-10-09T21:22:23.933 に答える
1

0 から 9 までの整数でリストを作成します (つまり、合計 10 項目)

List<Integer> l = ...
Collections.shuffle(l);
d1 = l.get(0);
d2 = l.get(1);
d3 = l.get(2);
d4 = l.get(3);
于 2013-08-19T15:23:20.110 に答える
1

これは、追加のデータ構造を使用せずに、一意の数字になるまで乱数の生成をループする私のソリューションです。

int a = 0, b = 0, c = 0, d = 0;
int x = 0;
while (true) {
    x = r.nextInt(9000) + 1000;
    a = x % 10;
    b = (x / 10) % 10;
    c = (x / 100) % 10;
    d = x / 1000;
    if (a == b || a == c || a == d || b == c || b == d || c == d)
        continue;
    else
        break;
}

System.out.println(x);
于 2013-08-19T15:25:57.513 に答える