<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