除了random函数,还有一个rand函数,也是一个随机函数,可以产生从0到rand_max的随机数。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
x = rand();
printf("%d\n", x);
return 0;
}
原來是因为随机数取在C语言中采用的是固定序列,所以每次执行所取的是同一个数。
但是,仔細想想,如果要生成一大堆不一樣的随机数,那要怎么做啊,跪求苍天啊啊啊啊啊啊~~~~ 那是没有用滴,还不如靠自己~~
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int x;
time_t t;
srand((unsigned) time(&t));
for(int i=0; i<10; i++) {
printf("%d\n", rand()%100); //生成10個0~99的不同隨機數
}
return 0;
}原文地址:http://blog.csdn.net/keshacookie/article/details/40214455