回答編集履歴
2
アルゴリズム修正
answer
CHANGED
@@ -14,4 +14,65 @@
|
|
14
14
|
|
15
15
|
```
|
16
16
|
|
17
|
+
ところで乱数発生の回数は確定させた方が良いです。
|
18
|
+
実行する度に実行時間が変わるとなると品質として難ありとなってしまうからです。
|
19
|
+
|
20
|
+
さらに乱数発生の回数を少なくした方がより高速になる傾向があります。(可読性とのトレードオフになってしまうため、実運用上は必ずしも少ないほど偉いというわけではありませんが)
|
21
|
+
|
22
|
+
乱数発生を一回で要件を満たすコードを書いてみました。
|
23
|
+
(昔教わった方法なので、念のためmainメソッドは確認用のコードとなっています)
|
24
|
+
|
25
|
+
良かったらご参考下さい。
|
26
|
+
|
27
|
+
訂正)消したり書いたりたびたびすみません。「rand /= (10 - i);」が抜けていました。確認方法として、変数randに乱数ではなく実際に1〜720の値を入れることで確認することができます!
|
28
|
+
|
29
|
+
```lang-java
|
30
|
+
public class Test {
|
31
|
+
public static void main(String[] args) {
|
32
|
+
int[][] result = new int[4][10];
|
33
|
+
for (int i = 0; i < 100000; i++) {
|
34
|
+
int[] arr = run();
|
35
|
+
result[0][arr[0]]++;
|
36
|
+
result[1][arr[1]]++;
|
37
|
+
result[2][arr[2]]++;
|
38
|
+
result[3][arr[3]]++;
|
39
|
+
}
|
40
|
+
// 各桁での出現回数を確認
|
41
|
+
System.out.println(Arrays.toString(result[0]));
|
42
|
+
System.out.println(Arrays.toString(result[1]));
|
43
|
+
System.out.println(Arrays.toString(result[2]));
|
44
|
+
System.out.println(Arrays.toString(result[3]));
|
45
|
+
}
|
46
|
+
public static int[] run() {
|
47
|
+
final int FIGURE = 4;
|
48
|
+
int answer[] = new int[FIGURE];
|
49
|
+
// 桁数FIGUREで, 取りうる値の総数を取得
|
50
|
+
int allNum = permutation(10, FIGURE);
|
51
|
+
// [0..9]のリスト
|
52
|
+
List<Integer> list = new ArrayList<>();
|
53
|
+
for (int i = 0; i < 10; i++) {
|
54
|
+
list.add(i);
|
55
|
+
}
|
56
|
+
// [1..allNum]までの整数を取得(乱数生成は1回だけ)
|
17
|
-
|
57
|
+
int rand = (int)(Math.floor(Math.random() * (allNum + 1))) + 1;
|
58
|
+
for (int i = 0; i < FIGURE; i++) {
|
59
|
+
// 剰余によりlistのインデックスを決定
|
60
|
+
// randは (FIGURE - i) の倍数のため,
|
61
|
+
// 剰余として現れる値は同様に確からしい
|
62
|
+
int index = rand % (10 - i);
|
63
|
+
answer[i] = list.get(index);
|
64
|
+
list.remove(index);
|
65
|
+
rand /= (10 - i);
|
66
|
+
}
|
67
|
+
return answer;
|
68
|
+
}
|
69
|
+
// 順列計算
|
70
|
+
public static int permutation(int n, int k) {
|
71
|
+
int result = 1;
|
72
|
+
for (int i = 0; i < k; i++) {
|
73
|
+
result *= ( n - i );
|
74
|
+
}
|
75
|
+
return result;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
```
|
1
誤ったアルゴリズムの記載。
answer
CHANGED
@@ -14,69 +14,4 @@
|
|
14
14
|
|
15
15
|
```
|
16
16
|
|
17
|
-
ところで乱数発生の回数は確定させた方が良いです。
|
18
|
-
実行する度に実行時間が変わるとなると品質として難ありとなってしまうからです。
|
19
|
-
|
20
|
-
さらに乱数発生の回数を少なくした方がより高速になる傾向があります。(可読性とのトレードオフになってしまうため、実運用上は必ずしも少ないほど偉いというわけではありませんが)
|
21
|
-
|
22
|
-
乱数発生を一回で要件を満たすコードを書いてみました。
|
23
|
-
(昔教わった方法なので、念のためmainメソッドは確認用のコードとなっています)
|
24
|
-
|
25
|
-
良かったらご参考下さい。
|
26
|
-
|
27
|
-
```lang-java
|
28
|
-
public class Test {
|
29
|
-
public static void main(String[] args) {
|
30
|
-
int[][] result = new int[4][10];
|
31
|
-
for (int i = 0; i < 100000; i++) {
|
32
|
-
int[] arr = run();
|
33
|
-
result[0][arr[0]]++;
|
34
|
-
result[1][arr[1]]++;
|
35
|
-
result[2][arr[2]]++;
|
36
|
-
result[3][arr[3]]++;
|
37
|
-
}
|
38
|
-
// 各桁での出現回数を確認
|
39
|
-
System.out.println(Arrays.toString(result[0]));
|
40
|
-
System.out.println(Arrays.toString(result[1]));
|
41
|
-
System.out.println(Arrays.toString(result[2]));
|
42
|
-
System.out.println(Arrays.toString(result[3]));
|
43
|
-
}
|
44
|
-
|
45
|
-
public static int[] run() {
|
46
|
-
final int FIGURE = 4;
|
47
|
-
int answer[] = new int[FIGURE];
|
48
|
-
|
49
|
-
// 桁数FIGUREで, 取りうる値の総数を取得
|
50
|
-
int allNum = permutation(10, FIGURE);
|
51
|
-
|
52
|
-
// [0..9]のリスト
|
53
|
-
List<Integer> list = new ArrayList<>();
|
54
|
-
for (int i = 0; i < 10; i++) {
|
55
|
-
list.add(i);
|
56
|
-
}
|
57
|
-
|
58
|
-
// [1..allNum]までの整数を取得(乱数生成は1回だけ)
|
59
|
-
|
17
|
+
訂正)すみません。以前ここに書いていたコードですが、誤りがあることが分かりました。誤解を招くため消去させていただきます。
|
60
|
-
|
61
|
-
for (int i = 0; i < FIGURE; i++) {
|
62
|
-
// 剰余によりlistのインデックスを決定
|
63
|
-
// randは (FIGURE - i) の倍数のため,
|
64
|
-
// 剰余として現れる値は同様に確からしい
|
65
|
-
int index = rand % (10 - i);
|
66
|
-
answer[i] = list.get(index);
|
67
|
-
list.remove(index);
|
68
|
-
}
|
69
|
-
|
70
|
-
return answer;
|
71
|
-
}
|
72
|
-
|
73
|
-
// 順列計算
|
74
|
-
public static int permutation(int n, int k) {
|
75
|
-
int result = 1;
|
76
|
-
for (int i = 0; i < k; i++) {
|
77
|
-
result *= ( n - i );
|
78
|
-
}
|
79
|
-
return result;
|
80
|
-
}
|
81
|
-
}
|
82
|
-
```
|