Latest web development tutorials

C library functions - srand ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionsvoid srand (unsigned int seed) sown by the function randusing a random number generator.

statement

Here is () statement srand function.

void srand(unsigned int seed)

parameter

  • seed - This is an integer value, the pseudo-random number generation algorithm for sowing.

return value

This function does not return a value.

Examples

The following example demonstrates the srand () function is used.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   /* 初始化随机数发生器 */
   srand((unsigned) time(&t));

   /* 输出 0 到 50 之间的 5 个随机数 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }
   
  return(0);
}

Let's compile and run the above program, which will result in the following:

38
45
29
29
47

C standard library - <stdlib.h> C standard library - <stdlib.h>