Ars*_*rel 4 c c++ random codeblocks
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
int i;
int diceRoll;
for(i=0; i < 20; i++)
{
printf("%d \n", rand());
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我在c(codeblocks)中编写的代码来获取随机数,问题是我总是得到相同的序列:41,18467,6334,26500等等...
我还在学习,所以请尝试解释,就像你正在和一个8岁的D谈话:
每次都会得到相同的序列,因为未设置随机数生成器的种子.你需要srand(time(NULL))像这样打电话:
int main()
{
srand(time(NULL));
....
Run Code Online (Sandbox Code Playgroud)