drand48()不是';t在我的c程序中工作
出于某种原因,我的drand48()都没有像预期的那样返回0-1之间的随机数。。。x只导致输入的数字为n,y保持为0drand48()不是';t在我的c程序中工作,c,random,posix,C,Random,Posix,出于某种原因,我的drand48()都没有像预期的那样返回0-1之间的随机数。。。x只导致输入的数字为n,y保持为0 /// Challenge: Darts /// circle of radius 0.5 inside a 2x2 square /// throw darts (x) times and calculate random hits #include <stdio.h> #include <math.h> #include <stdlib.h&g
/// Challenge: Darts
/// circle of radius 0.5 inside a 2x2 square
/// throw darts (x) times and calculate random hits
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
double n, t, z, a;
int x, y;
printf("How many darts do you want to throw? ");
scanf("%lf", &n);
printf("Okay, let's throw %lf darts\n. . .\n", n);
z = 0;
a = 0;
t = 0;
while (t <= n) {
x = drand48(); /// returns # 0-1
x = x - 0.5; /// puts it on a graph with domain/range -0.5 - 0.5
y = drand48();
y = y - 0.5;
if ((((x^2) + (y^2))^2) <= 0.25) {
z = z + 1;
}
t = t + 1;
}
a = z / (t - 1);
printf("%lf\n", a);
}
/// remember to compile with gcc darts.c -lm
/// remember to run with ./a.out
///挑战:飞镖
///2x2正方形内半径为0.5的圆
///掷镖(x)次并计算随机命中率
#包括
#包括
#包括
int main(){
双n,t,z,a;
int x,y;
printf(“你想投多少镖?”);
扫描频率(“%lf”,&n);
printf(“好的,让我们掷%lf镖\n…。\n”,n);
z=0;
a=0;
t=0;
虽然(t)提供一个.显示drand48
的定义,但它不是一个标准函数。@Olaf:它是一个Posix函数。x
和y
是int
,drand48()
返回一个double
。哎呀。很可能就是这样,对吧?谢谢x^2
没有做你可能认为它做的事情。我想你的意思是或x*x
。