Detailed description of how C ++ generates random numbers

Source: Internet
Author: User

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:

 
 
  1. # Include <iostream>
  2. # Include <ctime>
  3. # Include <cstdlib>
  4. Using namespace std;
  5. Int main ()
  6. {
  7. Double random (double, double );
  8. Srand (unsigned (time (0 )));
  9. For (int icnt = 0; icnt! = 10; ++ icnt)
  10. Cout <"No." <icnt + 1 <":" <
    Int (random (0, 10) <endl;
  11. Return 0;
  12. }
  13. Double random (double start, double end)
  14. {
  15. Return start + (end-start) * rand ()/(RAND_MAX + 1.0 );
  16. }
  17. /* Running result
  18. * No.1: 3
  19. * No. 2: 9
  20. * No. 3: 0
  21. * No. 4: 9
  22. * No. 5: 5
  23. * No. 6: 6
  24. * No. 7: 9
  25. * No. 8: 2
  26. * No. 9: 9
  27. * No. 10: 6
  28. */

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:

 
 
  1. # Include <iostream>
  2. # Include <ctime>
  3. # Include <cstdlib>
  4. # Include <iomanip>
  5. Using namespace std;
  6. Int main ()
  7. {
  8. Double random (double, double );
  9. Int a [10] = {0 };
  10. Const int gen_max= 10000000;
  11. Srand (unsigned (time (0 )));
  12. For (int icnt = 0; icnt! = Gen_max; ++ icnt)
  13. Switch (int (random (0, 10 )))
  14. {
  15. Case 0: a [0] ++; break;
  16. Case 1: a [1] ++; break;
  17. Case 2: a [2] ++; break;
  18. Case 3: a [3] ++; break;
  19. Case 4: a [4] ++; break;
  20. Case 5: a [5] ++; break;
  21. Case 6: a [6] ++; break;
  22. Case 7: a [7] ++; break;
  23. Case 8: a [8] ++; break;
  24. Case 9: a [9] ++; break;
  25. Default: cerr <"Error! "<Endl; exit (-1 );
  26. }
  27. For (int icnt = 0; icnt! = 10; ++ icnt)
  28. Cout <icnt <":" <setw (6) <
    Setiosflags (ios: fixed) <setprecision (2) <
    Double (a [icnt])/Gen_max * 100 <"%" <endl;
  29. Return 0;
  30. }
  31. Double random (double start, double end)
  32. {
  33. Return start + (end-start) * rand ()/(RAND_MAX + 1.0 );
  34. }
  35. /* Running result
  36. * 0: 10.01%
  37. * 1: 9.99%
  38. * 2: 9.99%
  39. * 3: 9.99%
  40. * 4: 9.98%
  41. * 5: 10.01%
  42. * 6: 10.02%
  43. * 7: 10.01%
  44. * 8: 10.01%
  45. * 9: 9.99%
  46. */

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 ++.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.