这是因为您没有声明类型lambda
,更正了结果在您寻求的范围内.如果未声明,旧编译器将认为它是int
.
#include#include #include #include double ran_expo(double lambda){ double u; u = rand() / (RAND_MAX + 1.0); return -log(1- u) / lambda; } int main(void) { int i; srand((unsigned)time(NULL)); for (i=0; i<20; i++) printf("%f\n", ran_expo(0.05)); return 0; }
节目输出:
0.025040 16.582459 4.296027 33.079902 17.589123 13.073084 8.624299 45.254803 34.611211 27.454302 3.825699 39.168172 24.790600 14.411160 7.247698 0.301951 1.917010 9.065004 3.187146 3.627885