/**
 * Main function to estimate the mean of exponential random variable
 * 
 * \param argc The number of aguments
 * \param argv The arguments provided to the program
 */
int main(int argc, char** argv) {

	// Check the arguments
	if (argc != 2) {
		printf("The correct syntax is: %s nb_runs\n", argv[0]);
		return ERROR_ENCOUNTERED;
	}

	// The result to return and the random value got
	double variance, random_x, random_y, random_z, distance, result = 0, nb_runs = atoi(argv[1]);

	// Runs nb_runs tries
	int i;
	for (i = 0; i < nb_runs; i++) {

		// Get 3 random variables
		random_x = MRG32k3a();
		random_y = MRG32k3a();
		random_z = MRG32k3a();

		// Get the distance of each points
		distance = sqrt(pow(0.5 - random_x, 2) + pow(0.5 - random_y, 2) + pow(0.5 - random_z, 2));

		// If less than the radius of the sphere
		if (distance <= 0.5) result += 1;
	}

	// Get the variance
	variance = sqrt((result - (nb_runs * pow((result / nb_runs), 2))) / (nb_runs - 1));

	// Divide by the number of tries
	result = result / nb_runs;

	// Display the result
	printf("Estimation of the mean: %lf.\n", result);
	printf("Confidence interval with 90 percent rate: [%lf, %lf].\n", result - C90*(variance/sqrt(nb_runs)), result + C90*(variance/sqrt(nb_runs)));
	printf("Confidence interval with 95 percent rate: [%lf, %lf].\n", result - C95*(variance/sqrt(nb_runs)), result + C95*(variance/sqrt(nb_runs)));
	printf("Confidence interval with 99 percent rate: [%lf, %lf].\n", result - C99*(variance/sqrt(nb_runs)), result + C99*(variance/sqrt(nb_runs)));

	// Exit correctly
	return EXECUTION_OK;
}
Beispiel #2
0
void main(int argc, char *argv[])

{
	int k=atoi(argv[1]);
	int i=0;
	double rand;
	double sum=0;

	while (i<k)
	{
		rand=(-log(MRG32k3a())/lambda);
		//printf("\nvalue is %lf", rand);
		i++;
		sum=sum+rand;
		
	}
	printf("Average is %lf", sum/k);
}