开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Dev-C++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
No
问题(Question):
设定一半径为500的圆,随机产生一点于圆内并算出与圆点的距离
现在有个问题就是,我的x1产生出来的值,没有办法像y1这么的随机
喂入的资料(Input):
No
预期的正确结果(Expected Output):
x1:-500~500的值,y1:-500~500的值
错误结果(Wrong Output):
x1感觉有固定的顺序,Ex:-433,-427,-420
程式码(Code):(请善用置底文网页, 记得排版)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int x1,y1;
float r1;
srand(time(NULL));
x1=(rand()%1000)-500;
y1=(rand()%1000)-500;
r1=sqrt(x1*x1+y1*y1);
if(r1<=500)
{
printf("x1 = %d\n",x1);
printf("y1 = %d\n",y1);
printf("r1 = %lf\n",r1);
}
else
{
printf("x1 = %d\n",x1);
printf("y1 = %d\n",y1);
printf("r1 is error\nr1 is %lf\n",r1);
}
}
补充说明(Supplement):
No
作者: yvb 2014-04-25 00:22:00
基本上 Dev-C++ 用了 MinGW, 就是用 MS-Windows 的 C-RuntimeDLLs,其中主要就是 MSVCRT.DLL (Microsoft C runtime library)而MSVC的 rand() 实作大概就是seed=seed*0x343fd+0x269EC3; return (seed>>0x10)&0x7FFF;因为 return 时做了 right-shift, 而 seed 的乘数不够大,所以造成srand()用相近的seed,第一次rand()会得到相近的值.