Generating random numbers and corresponding boxing guessing games

Keywords: C++

I. Introduction

In c + +, the common way to generate random numbers is to call the srand() and rand() functions. It is worth noting that the random number "generated" in all programs is actually a pseudo-random number, which is essentially a set of unordered numerical sequences stored in the computer.

Therefore, when initializing the random number seed, if the same seed value is initialized, the resulting "random number" will be the same value.

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 int main()
 5 {
 6     int i;
 7     srand(1);
 8     for (i = 0; i < 10; i++)
 9     {
10         cout << rand() << " ";
11     }
12 }

The result of the first run.

The result of the second run shows that it is not a real random number.

But when running the same program, you can think of these numbers as random numbers.

2, Generate random number

The key is to call the time function to initialize the random number seed so that the seed changes with time. The < CTime > header file needs to be called.

 1 #include <iostream>
 2 #include <ctime>
 3 #include <cstdlib>
 4 using namespace std;
 5 int main()
 6 {
 7     int i;
 8     srand((unsigned)time(NULL));
 9     for (i = 0; i < 10; i++)
10     {
11         cout << rand() << " ";
12     }
13     return 0;
14 }

Results of the first run.

The result of the second run can be regarded as a real random number.

When a random number of (0-x) is needed, it only needs to be in rand ()% X.

When (0-1) random number of decimals is needed, only (rand()% x) / X is needed, where x is related to precision and can be taken as two decimal places after x=100.

3, Guessing boxing games

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 int main()
 5 {
 6     char x;
 7     int a;
 8     int b;
 9     while (1) {
10         printf("Please select the one you want:\n");
11         printf("A:scissors\n");
12         printf("B:stone\n");
13         printf("C:cloth\n");
14         printf("D:No play.\n");
15 
16         scanf_s("%c%*c", &x);
17 
18         switch (x) {
19         case 65 | 97:
20             x = 4; break;
21         case 66 | 98:
22             x = 7; break;
23         case 67 | 99:
24             x = 10; break;
25         case 68 | 100:
26             return 0;
27         default:
28             printf("Error!");
29             getchar();
30             system("cls");
31             return 0;
32             break;
33         }
34         srand((unsigned)time(NULL));
35         a = rand() % 3;
36         b = (int)x + a;
37 
38         printf("The computer is out.");
39         switch (a) {
40         case 0:
41             printf("scissors\n\n"); break;
42         case 1:
43             printf("stone\n\n"); break;
44         case 2:
45             printf("cloth\n\n"); break;
46         }
47 
48         printf("You're out.");
49         switch (x) {
50         case 4:
51             printf("scissors\n\n"); break;
52         case 7:
53             printf("stone\n\n"); break;
54         case 10:
55             printf("cloth\n\n"); break;
56         }
57 
58         printf("The result is:\n\n");
59         if (b == 6 || b == 7 || b == 11)printf("You win!\n\n");
60         if (b == 5 || b == 9 || b == 10)printf("Computer wins!\n\n");
61         if (b == 4 || b == 8 || b == 12)printf("It ends in a draw!\n\n");
62 
63         system("pause>nul&&cls");
64     }
65     return 0;
66 }

For the first time.

The second time.

The value of x in the code is to consider the problem of non repetition.

Computer a | player x 4 scissors 7 stones 10 cloth
0 scissors 4 7 10
1 stones 5 8 11
2 cloth 6 9 12

Posted by FoTo50 on Tue, 31 Dec 2019 22:08:07 -0800