C + + random number generation

Source: Internet
Author: User

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ></pre><pre code_snippet_id= "617595" snippet_file_name= "blog_20150312_2_2284349" name= "code" class= " CPP ">                                      <pre name=" code "class=" CPP ">                                                                                    //The role of srand here is very important, the role of Srand () is to initialize the random number seed, the seed pseudo-random number calculation basis,//seed the same, The calculated random number is the same, usually the function is not used to srand () but with Rand (), it is equivalent to call the Srand (1),//This also solves the last semester when I do the final memory game, every time I rerun the program, the resulting random number is the same problem//time ( NULL) The value returned is the number of seconds since zero 1970.1.1, so every moment is different//#include <stdio.h> #include <time.h>//time () #include < Stdlib.h>//srand () +rand () int main () {Srand (Time (NULL));//Generate [A,b]int m = rand ()% a + b;printf ("%d\n", m);} </span></span>

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >//primarily generates random numbers, but the RAND () function generates a random number interval of [0,rand_max],//rand_max at least 32767 (2e15-1), and the values may differ under different conditions// Here the author uses an enlarged way to enlarge the interval (code from Rujia) #include <stdio.h> #include <time.h>//time () #include <stdlib.h>//srand ( ) +rand () int n = +, M = 100000;//This function is the real number within the [0,1] interval, and then expands (n-1) times in the next function to take the integer double random () {return (double) rand ()/Rand_ MAX;} int random (int m) {return (int) (Random () * (m-1) + 0.5);} int main () {Srand (Time (NULL));p rintf ("%d%d\n", N, M), for (int i = 0; i < m; i++) {if (rand ()% 2 = = 0) {printf ("A");} else{printf ("B");} int X, y;for (;;) {X = random (n) + 1; Y = Random (n) + 1;if (X! = Y) {break;}} printf ("%d%d\n", X, Y);} return 0;} </span></span>


C + + random number generation

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.