i want to write a C Program which reads d , l, n from the command line, where n
ID: 3728888 • Letter: I
Question
i want to write a C Program which reads d , l, n from the command line, where n is the number of trials.
i want to use drand48(). This should be used within a subroutine which initialises the random number generator just once (using a static variable) from the clock.
Buffon's needle problem is as follows. The top of a table (furniture) is marked off, dividing it into parallel strips of width d. A needle of length l is repeatedly tossed onto the table. Estimate the probability of the needle landing on one of the dividing lines. Assuming that lExplanation / Answer
Here is the estimation as per the formula
/*************/
/*
============================================================================
Name : RandomC.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
double d,l;
int n,N;
double y,theta;
int k=0;
double pi;
printf("Enter d,l,n ");
scanf("%lf%lf%d",&d,&l,&N);
n=N;
srand48(time(NULL)); //random number initialisation
while(n>0){
y=(int)(drand48()*100)%(int)d; //generate y
theta=(double)((int)(drand48()*1000)%157)/100; //geerate theta; 157 is pi/2*100
if(y<=l*sin(theta))
k++;
n--;
}
pi=2*l*N/(k*d);
printf("Pi: %f ",pi);
return EXIT_SUCCESS;
}
/************/output
Enter d,l,n 5 4 100000
Pi: 2.786534
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.