質問するログイン新規登録

回答編集履歴

3

注意書き追加

2020/01/02 07:36

投稿

jimbe
jimbe

スコア13382

answer CHANGED
@@ -103,4 +103,7 @@
103
103
 
104
104
  return 0;
105
105
  }
106
- ```
106
+ ```
107
+
108
+ なお, このコードでは整数化の為に "100分の1.5" 等ではなく "1000分の15" 等となっています.
109
+ つまり, 100 回やっても 1.5 回出るかどうかは分からないが 1000 回やれば必ず 15 回出ます.

2

コード追加

2020/01/02 07:36

投稿

jimbe
jimbe

スコア13382

answer CHANGED
@@ -1,3 +1,106 @@
1
1
  確率はあくまで可能性であって,「〇回やったら必ず決めた割合になる」とは言えないと思いますが.
2
2
 
3
- もし確率通り, 例えば10%で出るとなっていたら10回に1回は必ず出るようにするのでしたら, 福引のガラガラのように「出したモノは以降抽選から外す」ようにする等が必要かと思います.
3
+ もし確率通り, 例えば10%で出るとなっていたら10回に1回は必ず出るようにするのでしたら, 福引のガラガラのように「出したモノは以降抽選から外す」ようにする等が必要かと思います.
4
+
5
+ 以下は, ファイル読み込みやパラメータ(argv)によるモンスターの指定などは省いて(固定にして)います.
6
+ ```c
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ typedef struct data {
12
+ char name[32];
13
+ int probability; //整数化のため10倍値
14
+ } Data;
15
+ Data data[] = {
16
+ {"Dantalion", 15},
17
+ {"Lune", 15},
18
+ {"Silvi", 15},
19
+ {"Paimon", 15},
20
+ {"Fenrir-wiz", 15},
21
+ {"Yog-sothose", 15},
22
+ {"Madou", 15},
23
+ {"Echidna-SALA", 15},
24
+ {"JakotsuHime", 15},
25
+ {"Leeche", 15},
26
+ {"HakuMu", 15},
27
+ {"Zera", 15},
28
+ {"Kuzankoh", 15},
29
+ {"Salene", 15},
30
+ {"Valkyrie-CIEL",15},
31
+ {"Zeus-GIGA", 15},
32
+ {"Athena-NON", 15},
33
+ {"GaranDoushi", 15},
34
+ {"Fagan-RAI", 15},
35
+ {"Velois", 15},
36
+ {"Chutenmaru", 15},
37
+ {"Xerog-CORE", 15},
38
+ {"Minerva", 67},
39
+ {"Frey", 67},
40
+ {"Ison&Isuna", 67},
41
+ {"Andromeda", 67},
42
+ {"Bastet", 67},
43
+ {"Parvati", 67},
44
+ {"Venus", 67},
45
+ {"Ganesha", 67},
46
+ {"Tsukuyomi", 67},
47
+ {"Haku", 67},
48
+ {"", -1} //EOD
49
+ };
50
+
51
+ #define TIMES 1000000
52
+
53
+ int initLotteryArray(int *lotteryArray, int total) {
54
+ int i, j, k;
55
+
56
+ for(i=0, k=0; data[i].probability>0; i++) {
57
+ for(j=0; j<data[i].probability; j++, k++) lotteryArray[k] = i;
58
+ }
59
+ return total;
60
+ }
61
+
62
+ char *getMonster(int index, int *lotteryArray, int *lotteryCounts, int total) {
63
+ int i;
64
+ char *monster;
65
+
66
+ monster = data[lotteryArray[index]].name;
67
+ if(--(*lotteryCounts) <= 0) {
68
+ *lotteryCounts = initLotteryArray(lotteryArray, total);
69
+ } else {
70
+ //前詰め
71
+ for(i=index; i<*lotteryCounts; i++) lotteryArray[i] = lotteryArray[i+1];
72
+ }
73
+ return monster;
74
+ }
75
+
76
+ int getRandomInt(int lotteryCounts) {
77
+ return rand() % lotteryCounts;
78
+ }
79
+
80
+ int main() {
81
+ int i, total = 0, index;
82
+ int *lotteryArray;
83
+ int lotteryCounts;
84
+ char *monster;
85
+ int counter = 0;
86
+ double avg;
87
+
88
+ for(i=0; data[i].probability>0; i++) total += data[i].probability;
89
+
90
+ lotteryArray = (int *)malloc(sizeof(int)*total);
91
+ lotteryCounts = initLotteryArray(lotteryArray, total);
92
+
93
+ for(i=0; i<TIMES; i++){
94
+ index = getRandomInt(lotteryCounts);
95
+ monster = getMonster(index, lotteryArray, &lotteryCounts, total);
96
+ if(strcmp(monster, "Athena-NON") == 0) counter ++;
97
+ }
98
+
99
+ free(lotteryArray);
100
+
101
+ avg = (double)counter / (double)TIMES;
102
+ printf("排出率: %lf% 排出体数: %d体\n", avg * 100, counter);
103
+
104
+ return 0;
105
+ }
106
+ ```

1

追加

2020/01/02 07:07

投稿

jimbe
jimbe

スコア13382

answer CHANGED
@@ -1,1 +1,3 @@
1
- 確率はあくまで可能性であって,「〇回やったら必ず決めた割合になる」とは言えないと思いますが.
1
+ 確率はあくまで可能性であって,「〇回やったら必ず決めた割合になる」とは言えないと思いますが.
2
+
3
+ もし確率通り, 例えば10%で出るとなっていたら10回に1回は必ず出るようにするのでしたら, 福引のガラガラのように「出したモノは以降抽選から外す」ようにする等が必要かと思います.