永发信息网

C语言。。下边这个猜数字游戏运行不了,提示random是未定义的标识符

答案:4  悬赏:70  手机版
解决时间 2021-03-16 09:01
  • 提问者网友:却不属于对方
  • 2021-03-15 09:13
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{ int ia,ib;
long ic;
char ca,cb,cc;
time_t beg,end;
printf("Do you want to play this game?('y' or 'n')\n");
ca=getchar();
while(ca!='n')
{ ia=random(100);
printf("Now the system have produced a random number between 0 to 99,enter your guess:\n");
scanf("%d",&ib);
time(&beg);
while(ib!=ia)
{ if(ib>ia)
{ printf("please input a smaller number!\n");
}
if(ib<ia)
{ printf("please input a bigger number!\n");
}
scanf("%d",&ib);
}
time(&end);
ic=end-beg;
printf("Success!It took you %ld seconds to finish\n",ic);
{ if(ic<15)
{ printf("you are clever!\n");
}
else if(ic<25)
{ printf("you are normal!\n");
}
else
{ printf("you are stupid!\n");
}
}
getchar();
printf("Do you want to play this game?('y' or 'n')\n");
scanf("%c",&ca);
}
}
最佳答案
  • 五星知识达人网友:北方的南先生
  • 2021-03-15 09:58
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{ int ia,ib;
long ic;
char ca,cb,cc;
time_t beg,end;
printf("Do you want to play this game?('y' or 'n')\n");
ca=getchar();
while(ca!='n')
{
ia=rand()%100;
printf("Now the system have produced a random number between 0 to 99,enter your guess:\n");
scanf("%d",&ib);
time(&beg);
while(ib!=ia)
{ if(ib>ia)
{ printf("please input a smaller number!\n");
}
if(ib<ia)
{ printf("please input a bigger number!\n");
}
scanf("%d",&ib);
}
time(&end);
ic=end-beg;
printf("Success!It took you %ld seconds to finish\n",ic);
{ if(ic<15)
{ printf("you are clever!\n");
}
else if(ic<25)
{ printf("you are normal!\n");
}
else
{ printf("you are stupid!\n");
}
}
getchar();
printf("Do you want to play this game?('y' or 'n')\n");
scanf("%c",&ca);
}
}

在vc++中程序中用了srandom()和random(),头文件为stdlib.h,但编译出现错误error C3861: “srandom”: 找不到标识符。
原因是现在vc++编译器的库函数中没有randomize()和random(),分别用srand()和rand()代替了。
将random函数改成rand()函数就好了
全部回答
  • 1楼网友:人间朝暮
  • 2021-03-15 13:10
用的VC环境吧,VC环境中没有这个函数的,实际上random()是C++ BUILD 里面的一个函数,在vc中一般用 srand() 和rand()。这两个函数的基本用法。。 (1) 如果你只要产生随机数而不需要设定范围的话,你只要用rand()就可以了:rand()会返回一随机数值, 范围在0至RAND_MAX 间。RAND_MAX定义在stdlib.h, 其值为2147483647。 (2) 如果你要随机生成一个在一定范围的数,你可以在宏定义中定义一个random(int number)函数,然后在main()里面直接调用random()函数 (3)但是上面两个例子所生成的随机数都只能是一次性的,如果你第二次运行的时候输出结果仍和第一次一样。这与srand()函数有关。srand()用来设置rand()产生随机数时的随机数种子。在调用rand()函数产生随机数前,必须先利用srand()设好随机数种子(seed),如果未设随机数种子, rand()在调用时会自动设随机数种子为1。如果没有设置随机数种子,每次随机数种子都自动设成相同值1 ,进而导致rand()所产生的随机数值都一样。 srand()函数定义 :void srand (unsigned int seed);  通常可以利用geypid()或time(0)的返回值来当做seed 如果你用time(0)的话,要加入头文件#include<time.h> 简单的例子: #include "time.h" #include "stdlib.h" int main(){  srand(time(NULL)); i = rand() % 10 ;//可取0-9的数  return 0; }
  • 2楼网友:我住北渡口
  • 2021-03-15 11:42
C语言没有random函数吧 是rand函数。
  • 3楼网友:行雁书
  • 2021-03-15 10:07
C语言没有random函数吧 是rand函数。 再看看别人怎么说的。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯