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

質問編集履歴

3

2018/03/09 10:52

投稿

Rock_s6nmnl
Rock_s6nmnl

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,36 @@
1
- rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
1
+ rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <time.h>
6
+ #include <stdbool.h>
7
+
8
+ int main(void) {
9
+ srand((unsigned)time(NULL));
10
+ //trueになったら死ぬ、金が手に入る
11
+ bool m_flag = false, d_flag = false;
12
+ //死ぬ確率
13
+ int num_pro_d = rand() % 100 + 1;
14
+ //何万円もらえるか
15
+ int num_pro_m = 100 - num_pro_d;
16
+
17
+
18
+ printf("%d%%の確率で死ぬ\n",num_pro_d);
19
+ printf("%d%%の確率で%d万円手に入る\n",num_pro_m, num_pro_m);
20
+
21
+ double tmp_random = (double)rand()/RAND_MAX;
22
+ if (tmp_random < (double)num_pro_d/100) {
23
+ d_flag = true;
24
+ }
25
+ if (1 - tmp_random < (double)num_pro_d/100) {
26
+ m_flag = true;
27
+ }
28
+
29
+ if(m_flag == true){
30
+ printf("%d万円手に入れた\n",num_pro_m);
31
+ }
32
+ if(d_flag == true){
33
+ printf("%d%%の確率で死んだ\n",num_pro_d);
34
+ }
35
+ return 0;
36
+ }

2

2018/03/09 10:52

投稿

Rock_s6nmnl
Rock_s6nmnl

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,36 +1,1 @@
1
- rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
1
+ rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
2
-
3
- #include <stdio.h>
4
- #include <stdlib.h>
5
- #include <time.h>
6
- #include <stdbool.h>
7
-
8
- int main(void) {
9
- srand((unsigned)time(NULL));
10
- //trueになったら死ぬ、金が手に入る
11
- bool m_flag = false, d_flag = false;
12
- //死ぬ確率
13
- int num_pro_d = rand() % 100 + 1;
14
- //何万円もらえるか
15
- int num_pro_m = 100 - num_pro_d;
16
-
17
-
18
- printf("%d%%の確率で死ぬ\n",num_pro_d);
19
- printf("%d%%の確率で%d万円手に入る\n",num_pro_m, num_pro_m);
20
-
21
- double tmp_random = (double)rand()/RAND_MAX;
22
- if (tmp_random < (double)num_pro_d/100) {
23
- d_flag = true;
24
- }
25
- if (1 - tmp_random < (double)num_pro_d/100) {
26
- m_flag = true;
27
- }
28
-
29
- if(m_flag == true){
30
- printf("%d万円手に入れた\n",num_pro_m);
31
- }
32
- if(d_flag == true){
33
- printf("%d%%の確率で死んだ\n",num_pro_d);
34
- }
35
- return 0;
36
- }

1

2018/03/09 10:38

投稿

Rock_s6nmnl
Rock_s6nmnl

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,1 +1,36 @@
1
- rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
1
+ rand関数で1から100までの乱数を生成して、もし30なら30%の確率で任意の処理をさせるコードを教えてください。
2
+
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <time.h>
6
+ #include <stdbool.h>
7
+
8
+ int main(void) {
9
+ srand((unsigned)time(NULL));
10
+ //trueになったら死ぬ、金が手に入る
11
+ bool m_flag = false, d_flag = false;
12
+ //死ぬ確率
13
+ int num_pro_d = rand() % 100 + 1;
14
+ //何万円もらえるか
15
+ int num_pro_m = 100 - num_pro_d;
16
+
17
+
18
+ printf("%d%%の確率で死ぬ\n",num_pro_d);
19
+ printf("%d%%の確率で%d万円手に入る\n",num_pro_m, num_pro_m);
20
+
21
+ double tmp_random = (double)rand()/RAND_MAX;
22
+ if (tmp_random < (double)num_pro_d/100) {
23
+ d_flag = true;
24
+ }
25
+ if (1 - tmp_random < (double)num_pro_d/100) {
26
+ m_flag = true;
27
+ }
28
+
29
+ if(m_flag == true){
30
+ printf("%d万円手に入れた\n",num_pro_m);
31
+ }
32
+ if(d_flag == true){
33
+ printf("%d%%の確率で死んだ\n",num_pro_d);
34
+ }
35
+ return 0;
36
+ }