開發平台(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()會得到相近的值.