/* * @@ exp ϕ */ #include #include #include #define RAND() ((double)rand() / (1.0 + RAND_MAX)) int main(void) { int seed; int j; int ntrial; int n1 = 0; double x0, y0; double s; double sreal, serror; printf("Input: Number of trials:\n"); scanf("%u", &ntrial); printf("Input: seed\n"); scanf("%u", &seed); srand(seed); for (j = 1; j <= ntrial; j ++) { x0 = RAND(); y0 = RAND(); if (y0 < exp(-x0)) { n1 = n1 + 1; } } s = (double)n1 / ntrial; sreal=1.0-exp(-1.0); serror=s-sreal; printf("s = %1.10f \n", s); printf("sreal = %1.10f \n", sreal); printf("serror = %1.10f \n", serror); }