C产生随机数

1.伪随机数的产生


计算机中的伪随机数的产生依赖于随机数发生器和随机数生成函数.

2. C语言中的函数(stdlib.h)


  • 随机数生成示例
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define K 101
int main()
{
    int a,b;
    srand((unsigned)time(NULL));   //随机数生成器,当前系统时间做种子.  time()在time.h头文件中
    //void srand (unsigned int);   //在stdlib.h中的声明
    a=rand();                      //随机数产生函数.
    //int rand (void);             //在stdlib.h中的声明
    //#define RAND_MAX  0x7FFF     stdlib.h中声明rand();产生的最大值
    b=rand()%K;                     //产生位于[0,K-1]的整数.

    printf("%d\n%d\n",a,b);
    return 0;
}

Code::Blocks上结果如图:
Code::Blocks

猜你喜欢

转载自blog.csdn.net/m0_38062488/article/details/80396812