Requirement: Random number between output [X, Y]
Algorithm: rand ()% (y-x+1) +x
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/ time.h>//interchange function Void swap (INT&NBSP;*A,&NBSP;INT&NBSP;*B) { int temp= 0; temp = *a; *a = *b; *b = temp;} The sum of the corresponding arrays of the two random numbers [20,40] array is stored in the third array. And do bubble sort. Int main (void) { int a[10] = {0}, b[10] = {0}, c[10] = {0}; int i = 0, j = 0; Struct timeval timenow; gettimeofday (&timenow, null); srand (timenow.tv_usec); //the current time, the microsecond level as a seed. for (i = 0; i < 10; i++) { a[i] = rand ()%21+20; b[i] = Rand ()%21+20; c[i] = a[i] + b[i]; printf ("%d + %d = %d\n", A[i],b[i],c[i]); } printf ("Start bubbling sort: \ n"); for (i = 0; i< 10 -1; i++) { for (j = 0; j< 10 -1 -i; j++) { if (c[j] < c[j+1]) swap (&c[j], &c[j+1]); } } for (i = 0; i < 10; i++) { printf ("c[%d] = %d\n", I,c[i]); } return 0;}
This article is from the "Imperial Gold Cai Yuan zi" blog, please be sure to keep this source http://diguojin.blog.51cto.com/5034509/1614373
Summary of pseudo-random numbers in C language