回答編集履歴

3

注意書き追加

2020/01/02 07:36

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -209,3 +209,9 @@
209
209
  }
210
210
 
211
211
  ```
212
+
213
+
214
+
215
+ なお, このコードでは整数化の為に "100分の1.5" 等ではなく "1000分の15" 等となっています.
216
+
217
+ つまり, 100 回やっても 1.5 回出るかどうかは分からないが 1000 回やれば必ず 15 回出ます.

2

コード追加

2020/01/02 07:36

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -3,3 +3,209 @@
3
3
 
4
4
 
5
5
  もし確率通り, 例えば10%で出るとなっていたら10回に1回は必ず出るようにするのでしたら, 福引のガラガラのように「出したモノは以降抽選から外す」ようにする等が必要かと思います.
6
+
7
+
8
+
9
+ 以下は, ファイル読み込みやパラメータ(argv)によるモンスターの指定などは省いて(固定にして)います.
10
+
11
+ ```c
12
+
13
+ #include <stdio.h>
14
+
15
+ #include <stdlib.h>
16
+
17
+ #include <string.h>
18
+
19
+
20
+
21
+ typedef struct data {
22
+
23
+ char name[32];
24
+
25
+ int probability; //整数化のため10倍値
26
+
27
+ } Data;
28
+
29
+ Data data[] = {
30
+
31
+ {"Dantalion", 15},
32
+
33
+ {"Lune", 15},
34
+
35
+ {"Silvi", 15},
36
+
37
+ {"Paimon", 15},
38
+
39
+ {"Fenrir-wiz", 15},
40
+
41
+ {"Yog-sothose", 15},
42
+
43
+ {"Madou", 15},
44
+
45
+ {"Echidna-SALA", 15},
46
+
47
+ {"JakotsuHime", 15},
48
+
49
+ {"Leeche", 15},
50
+
51
+ {"HakuMu", 15},
52
+
53
+ {"Zera", 15},
54
+
55
+ {"Kuzankoh", 15},
56
+
57
+ {"Salene", 15},
58
+
59
+ {"Valkyrie-CIEL",15},
60
+
61
+ {"Zeus-GIGA", 15},
62
+
63
+ {"Athena-NON", 15},
64
+
65
+ {"GaranDoushi", 15},
66
+
67
+ {"Fagan-RAI", 15},
68
+
69
+ {"Velois", 15},
70
+
71
+ {"Chutenmaru", 15},
72
+
73
+ {"Xerog-CORE", 15},
74
+
75
+ {"Minerva", 67},
76
+
77
+ {"Frey", 67},
78
+
79
+ {"Ison&Isuna", 67},
80
+
81
+ {"Andromeda", 67},
82
+
83
+ {"Bastet", 67},
84
+
85
+ {"Parvati", 67},
86
+
87
+ {"Venus", 67},
88
+
89
+ {"Ganesha", 67},
90
+
91
+ {"Tsukuyomi", 67},
92
+
93
+ {"Haku", 67},
94
+
95
+ {"", -1} //EOD
96
+
97
+ };
98
+
99
+
100
+
101
+ #define TIMES 1000000
102
+
103
+
104
+
105
+ int initLotteryArray(int *lotteryArray, int total) {
106
+
107
+ int i, j, k;
108
+
109
+
110
+
111
+ for(i=0, k=0; data[i].probability>0; i++) {
112
+
113
+ for(j=0; j<data[i].probability; j++, k++) lotteryArray[k] = i;
114
+
115
+ }
116
+
117
+ return total;
118
+
119
+ }
120
+
121
+
122
+
123
+ char *getMonster(int index, int *lotteryArray, int *lotteryCounts, int total) {
124
+
125
+ int i;
126
+
127
+ char *monster;
128
+
129
+
130
+
131
+ monster = data[lotteryArray[index]].name;
132
+
133
+ if(--(*lotteryCounts) <= 0) {
134
+
135
+ *lotteryCounts = initLotteryArray(lotteryArray, total);
136
+
137
+ } else {
138
+
139
+ //前詰め
140
+
141
+ for(i=index; i<*lotteryCounts; i++) lotteryArray[i] = lotteryArray[i+1];
142
+
143
+ }
144
+
145
+ return monster;
146
+
147
+ }
148
+
149
+
150
+
151
+ int getRandomInt(int lotteryCounts) {
152
+
153
+ return rand() % lotteryCounts;
154
+
155
+ }
156
+
157
+
158
+
159
+ int main() {
160
+
161
+ int i, total = 0, index;
162
+
163
+ int *lotteryArray;
164
+
165
+ int lotteryCounts;
166
+
167
+ char *monster;
168
+
169
+ int counter = 0;
170
+
171
+ double avg;
172
+
173
+
174
+
175
+ for(i=0; data[i].probability>0; i++) total += data[i].probability;
176
+
177
+
178
+
179
+ lotteryArray = (int *)malloc(sizeof(int)*total);
180
+
181
+ lotteryCounts = initLotteryArray(lotteryArray, total);
182
+
183
+
184
+
185
+ for(i=0; i<TIMES; i++){
186
+
187
+ index = getRandomInt(lotteryCounts);
188
+
189
+ monster = getMonster(index, lotteryArray, &lotteryCounts, total);
190
+
191
+ if(strcmp(monster, "Athena-NON") == 0) counter ++;
192
+
193
+ }
194
+
195
+
196
+
197
+ free(lotteryArray);
198
+
199
+
200
+
201
+ avg = (double)counter / (double)TIMES;
202
+
203
+ printf("排出率: %lf% 排出体数: %d体\n", avg * 100, counter);
204
+
205
+
206
+
207
+ return 0;
208
+
209
+ }
210
+
211
+ ```

1

追加

2020/01/02 07:07

投稿

jimbe
jimbe

スコア12659

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