在C语言中如何产生随机数

如题所述

假设要生成的随机数是大于等于20且小于等于100的整数

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void main() { int a,b,x;
  a=20; b=100;
  srand((unsigned int)time(NULL)); //用当前时间生成一个随机数种子
  x=rand()%(b-a+1)+a;
  printf("得到一个随机数:%d\n",x);
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答