The Application of C ++ programming language can easily help developers achieve various functional requirements, such as the generation of random numbers. Here we will give you a detailed introduction to the specific methods for generating random numbers in C ++. I hope you can have a detailed understanding of this.
In C ++, the rand () function is often used to generate random numbers, but strictly speaking, the pseudo-random number is only pseudo-do-random integral number ). When generating a random number, we need to specify a seed. If the program loops, the next time the random number is generated, the result will be called as the seed. However, if the program is executed twice, the generated "random number" is the same because the seeds are the same.
In engineering applications, we generally use the current system time (Unix time) as the seed, so that C ++ generates random numbers closer to actual random numbers. The example is as follows:
- # Include <iostream>
- # Include <ctime>
- # Include <cstdlib>
- Using namespace std;
- Int main ()
- {
- Double random (double, double );
- Srand (unsigned (time (0 )));
- For (int icnt = 0; icnt! = 10; ++ icnt)
- Cout <"No." <icnt + 1 <":" <
Int (random (0, 10) <endl;
- Return 0;
- }
- Double random (double start, double end)
- {
- Return start + (end-start) * rand ()/(RAND_MAX + 1.0 );
- }
- /* Running result
- * No.1: 3
- * No. 2: 9
- * No. 3: 0
- * No. 4: 9
- * No. 5: 5
- * No. 6: 6
- * No. 7: 9
- * No. 8: 2
- * No. 9: 9
- * No. 10: 6
- */
Can I use this C ++ method to generate a random number to obtain a random number in the full sense? It seems that there are more than nine? But there are no, 7 ?! Let's do a probability experiment to generate 10 million random numbers. Check whether the frequencies of the 10 numbers 0-9 are roughly the same. The procedure is as follows:
- # Include <iostream>
- # Include <ctime>
- # Include <cstdlib>
- # Include <iomanip>
- Using namespace std;
- Int main ()
- {
- Double random (double, double );
- Int a [10] = {0 };
- Const int gen_max= 10000000;
- Srand (unsigned (time (0 )));
- For (int icnt = 0; icnt! = Gen_max; ++ icnt)
- Switch (int (random (0, 10 )))
- {
- Case 0: a [0] ++; break;
- Case 1: a [1] ++; break;
- Case 2: a [2] ++; break;
- Case 3: a [3] ++; break;
- Case 4: a [4] ++; break;
- Case 5: a [5] ++; break;
- Case 6: a [6] ++; break;
- Case 7: a [7] ++; break;
- Case 8: a [8] ++; break;
- Case 9: a [9] ++; break;
- Default: cerr <"Error! "<Endl; exit (-1 );
- }
- For (int icnt = 0; icnt! = 10; ++ icnt)
- Cout <icnt <":" <setw (6) <
Setiosflags (ios: fixed) <setprecision (2) <
Double (a [icnt])/Gen_max * 100 <"%" <endl;
- Return 0;
- }
- Double random (double start, double end)
- {
- Return start + (end-start) * rand ()/(RAND_MAX + 1.0 );
- }
- /* Running result
- * 0: 10.01%
- * 1: 9.99%
- * 2: 9.99%
- * 3: 9.99%
- * 4: 9.98%
- * 5: 10.01%
- * 6: 10.02%
- * 7: 10.01%
- * 8: 10.01%
- * 9: 9.99%
- */
It is known that the random number obtained by this method meets the statistical rule. The above describes how to generate random numbers in C ++.