rand函数

来源:互联网 发布:数组里的频率最高点 编辑:程序博客网 时间:2024/05/16 12:58
MSDN中关于rand的描述"The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767). Use the srand function to seed the pseudorandom-number generator before calling rand."rand()产生的伪随机数的范围是0到32767,一般想要产生比如[5,125]的随机数,可以这么写:int x = rand()%120 + 5;其中120是区间长度。rand()产生的随机数是“伪随机数”,就是有可能在你的机器上运行,每次运行程序产生的随机数序列都是一样的,因此需要使用srand()解决这个问题:#include <time.h>srand(time(NULL));   //这句放在任何的rand()之前,且只需使用一次
原创粉丝点击