Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C++ generates random numbers
C++ generates random numbers
This article was originally written by Song Qing and published according to GPL-V2 and its subsequent versions. Please indicate the source and attach our statement. The rand () function is often used to generate random numbers in \ x0d \ x0d \ c++, but strictly speaking, it only generates pseudo-random integers. When generating random numbers, we need to specify a seed. If we loop through the program, the last result will be called as the seed the next time we generate a random number. However, if the program is executed twice, the generated "random numbers" are the same because the seeds are the same. \x0d\\x0d\ In engineering applications, we usually take the current system time (Unix time) as the seed, so that the generated random number is closer to the actual random number. Give the following routine: \ x0d \ \ x0d \ # include \ x0d \ # include \ x0d \ # include \ x0d \ use the namespace STD \ x0d \ \ x0d \ int main () \ x0d \ {\ x0d \ doublerandom (double, \ x0d \ srand(unsigned(time(0))); \ x0d \ for(int icnt = 0; icnt! = 10; ++ icnt)\ x0d \ cout & lt; & lt "no".<< icnt+1< & lt":" & lt& ltint(random(0, 10))& lt; & ltendl\x0d\ returns 0; \ x0d \ } \ x0d \ \ x0d \ double random(double start,double end)\ x0d \ { \ x0d \ return start+(end-start)* RAND()/(RAND _ MAX+ 1.0); \x0d\}\x0d\/* Operation result \ x0d \ * No.65438 +0: 3 \ x0d \ * No.2: 9 \ x0d \ * No.3: 0 \ x0d \ * No.4: 9 \ x0d \ * No.9. Nine seems a bit much, doesn't it? No 1, 4, 7? ! Let's do a probability experiment and generate 100000 random numbers to see if the frequencies of these10 numbers from 0 to 9 are roughly the same. The program is as follows: \ x0d \ # include \ x0d \ # include \ x0d \ # include \ x0d \ use the namespace STD \ x0d \ \ x0d \ int main () \ x0d \ {\ x0d \ doublerandom (. \ x0d \ int a[ 10]=; \ x0d \ const int Gen _ max = 1000000; \ x0d \ srand(unsigned(time(0))); \ x0d \ \ x0d \ for(int icnt = 0; icnt! = Gen _ max++icnt)\ x0d \ switch(int(random(0, 10))\ x0d \ { \ x0d \ case 0:a[0]++; Break; \ x0d \ case 1:a[ 1]++; Break; \ x0d \ case 2:a[2]++; Break; \ x0d \ case 3:a[3]++; Break; \ x0d \ case 4:a[4]++; Break; \ x0d \ case 5:a[5]++; Break; \x0d\ Case 6: A [6]++; Break; \ x0d \ case 7:a[7]++; Break; \ x0d \ case 8:a[8]++; Break; \ x0d \ case 9:a[9]++; Break; \ x0d \ default:cerr & lt; & lt "Error!" & lt& ltendl exit (-1); \ x0d \ } \ x0d \ \ x0d \ for(int icnt = 0; icnt! = 10; ++ icnt)\ x0d \ cout & lt; & lticnt & lt& lt":" & lt& ltsetw(6)& lt; & ltsetios flags(IOs::fixed)& lt; & ltsetprecision(2)& ltdouble(a[icnt])/Gen _ max * 100 & lt; & lt“%”& lt; & ltendl\ x0d \ \ x0d \ return 0; \ x0d \ } \ x0d \ \ x0d \ double random(double start,double end)\ x0d \ { \ x0d \ return start+(end-start)* RAND()/(RAND _ MAX+ 1.0); \x0d\}\x0d\/* Operation result \ x0d \ * 0:10.01%\ x0d \ *1:9.99% \ x0d \ * 2: 9.99% \ x0d \ * 6: 10.02% \ x0d \ * 7: 10.0 1% \ x0d \ * 8: 10.0 1% \ x0d \ * 9:9.99。 \x0d\\x0d\ In addition, the program is compiled with GCC under Linux. Even if I perform 1000000 operations, whether the random function is defined as inline function seems to have no effect on the program. There is reason to believe that GCC has been optimized for us. But somehow, I remember that O3 is needed to optimize inlining ... \x0d\\x0d\ can't, so we changed the number of cycles to 65.438+0 billion, and checked the execution time with the time command: \ x0d \ Chinsung @ gentoo ~/workspace/test/debug $ time. /test \ x0d \ 0: 10.00% \ x0d \ 1: 10.00% \ x0d \ 2: 10.00% \ x0d \ 3: 10.00% \ x0d \ 4: 10.00% \ x0d \ 5: 10.00% \ x0d \ 6:65433/test \ x0d \ 0: 10.00% \ x0d \ 1: 10.00% \ x0d \ 2: 10.00% \ x0d \ 3: 10.00% \ X0d \ 4:10.00% \ x0d \ 5:10.00% \ x0d \ 6: 65433 There is little difference between the two results, and even the latter indicators are better. I don't know why. ...