VC中产生随机数

需要的头文件:<stdlib.h>,<time.h>

库函数:srand;rand;time

方法:1.首先设置种子srand(unsigned)time(NULL));使用当前时间作为种子是多数人的习惯做法.
         2.产生随机数:rand()可以产生一个随机数;范围在0~RAND_MAX(32767)之间;如果要产生一个[min,max]之间的数,可以这样:rand()%(max) + min;

例子:产生10个[0,99]之间的随机整数:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
main()
{
    int i;
srand(time(0));/*设置种子,并生成伪随机序列*/
for(i=0;i<N;++i)
   printf("%d ",rand()%100);/*得到[0,99]伪随机数*/
   system("pause");
}

原文地址:https://www.cnblogs.com/hzcya1995/p/13318802.html