当前位置: 代码迷 >> C语言 >> 谁解释一下!!!
  详细解决方案

谁解释一下!!!

热度:252   发布时间:2005-11-20 18:11:00.0
谁解释一下!!!
运行这个后 为什么每次rand()所选的随机数是一样的都是41
!!!????


#include <stdio.h>
#include <stdlib.h>
void main()
{
int m,n;
m= rand();
printf("Please guess a number:");
scanf("%d",&n);
if (n> m)
{
printf("Too high");
}
else if (n <m)
{
printf("Too low");
}
else
{
printf("Right \n");
printf("The number m is :%d\n",m);
}
}
搜索更多相关的解决方案: 解释  

----------------解决方案--------------------------------------------------------
种子没改变所以每次都是一样的伪随机数
----------------解决方案--------------------------------------------------------

谢谢!知道了!
因为提供的随机种子数相同 所以不变
可以使用计算机读取其时时钟并把该值自动设置成随机种子数 用这个语句
srand (time (NULL) );
搞定!!


----------------解决方案--------------------------------------------------------

我是以时间为种子,

你可能是要编那种猜数字的游戏吧?你程序都写错了
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
void main()
{
time_t t;
int m,n;
srand((unsigned) time(&t));
m= rand();
printf("Please guess a number:");
scanf("%d",&n);

while(n!=m)
{
if (n> m)
{
printf("Too high");
printf("Please guess a number:");
scanf("%d",&n);

}
else if (n <m)
{
printf("Too low");
printf("Please guess a number:");
scanf("%d",&n);
}
}

if(m=n)
{printf("Right \n");
printf("The number m is :%d\n",m);
}
}


----------------解决方案--------------------------------------------------------
这样写可以吗?


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int m,n;
srand(time(NULL));
m= rand();
printf("Please guess a number:");
scanf("%d",&n);
if (n> m)
{
printf("Too high\n");
}
else if (n <m)
{
printf("Too low\n");
}
else
{
printf("Right \n");
printf("The number m is :%d\n",m);
}
}
----------------解决方案--------------------------------------------------------
我想我的程序应该把srand(time(NULL));
改成 srand( (unsigned)time( NULL ) );
更合适 对吧!
----------------解决方案--------------------------------------------------------
序都写错了
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
void main()
{
time_t t;
int m,n;
srand((unsigned) time(&t));
m= rand();
printf("Please guess a number:");
scanf("%d",&n);

while(n!=m)
{
if (n> m)
{
printf("Too high");
printf("Please guess a number:");
scanf("%d",&n);

}
else if (n <m)
{
printf("Too low");
printf("Please guess a number:");
scanf("%d",&n);
}
}

if(m=n)
{printf("Right \n");
printf("The number m is :%d\n",m);
}
}这还是真的啊


----------------解决方案--------------------------------------------------------
序都写错了
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
void main()
{
time_t t;
int m,n;
srand((unsigned) time(&t));
m= rand();
printf("Please guess a number:");
scanf("%d",&n);

while(n!=m)
{
if (n> m)
{
printf("Too high");
printf("Please guess a number:");
scanf("%d",&n);

}
else if (n <m)
{
printf("Too low");
printf("Please guess a number:");
scanf("%d",&n);
}
}

if(m=n)
{printf("Right \n");
printf("The number m is :%d\n",m);
}
}还是真的啊


----------------解决方案--------------------------------------------------------
为什么要三个这样的#include <stdio.h>
高手们可以说说吗
----------------解决方案--------------------------------------------------------
楼上具体什么意思?
是问头文件的作用吗?
----------------解决方案--------------------------------------------------------