Problem with Monte Carlo(integration by rejection)

Printable View

• 04-06-2004
dionys
Problem with Monte Carlo(integration by rejection)
This program finds the value of π (pi) with the Monte Carlo method.
#1Consiter that unit circle (radious = 1 ) within a square with sides equal to 2.If we pick a random point(x,y) were both x and y are
between -1,........1. the probability that this random point lies
inside the circle is given as the proportion:
P( x^2 + y^2<1 ) = Area circle/Area square = ( pi )/4

#2If we pick a random point N times and M of those times the point lies inside the unit circle ,the probabillity of that a random point lies inside the unit circle is given as:
P ( x^2 + y^2<1 ) = M/N

#3 But if N becomes very large (theoretically infinite)the true probabilities #1-#2 tend to became equal
4M/N=pi
Can you help me in my problem. I'm new with the programming :)
I want to make drand48() function tou give me values in the interval [-1,1]
and i dont know how....

Code:

```#include<stdio.h> #include<stdlib.h> #define TOTAL_NUM 100000 int m=0,n=0;double x,y,pi; int main() {     for(n=0;n<TOTAL_NUM;n++)         {           x=............i need some code here;           y=............also i need some code here;           if((x*x)+(y*y)<1) m++;           } pi=4.0*m/n; printf("%lf",pi) return 0; }```
• 04-06-2004
Thantos
Since I'm in a generous mood:

Function to return a random float between two numbers:
Code:

```float getrandomfloat(float min, float max) {   float diff = max - min;   float temp;   temp = (float)rand() / RAND_MAX;   temp *= diff;   temp += min;   return temp; }```
• 04-06-2004
dionys
why i get this error?(Monte carlo method)
This program finds the value of ð (pi) with the Monte Carlo method.
#1Consiter that unit circle (radious = 1 ) within a square with sides equal to 2.If we pick a random point(x,y) were both x and y are
between -1,........1. the probability that this random point lies
inside the circle is given as the proportion:
P( x^2+y^2<1 ) = Area circle/Area square = ( pi )/4

#2If we pick a random point N times and M of those times the point lies inside the unit circle ,the probabillity of that a random point lies inside the unit circle is given as:
P ( x^2+y^2<1 ) = M/N

#3 But if N becomes very large (theoretically infinite)the true probabilities #1-#2 tend to became equal
4M/N=pi

WHY I GET THIS ERROR:undefined reference to `drand48(void)

Code:

```#include<stdio.h> #include<stdlib.h> double x,y,pi,n_counter,count,m; double drand48(void); int main() { printf("Give N :\n"); scanf("%lf",&n_counter);       for(count=0;count<=n_counter;count++)       {         y=2.0*drand48()-1.0;         x=2.0*drand48()-1.0;           if(x*x+y*y<1)             {                 m++;             } } pi=4.0*m/n_counter; printf("Pi =%lf",pi); return 0; }```
• 04-06-2004
Thantos
1) Keep all post related to the same program in the same thread:
http://cboard.cprogramming.com/showthread.php?t=51483

2) From what you've posted you haven't actually defined drand48() anywhere.
• 04-07-2004
Salem
Threads merged
• 04-07-2004
dionys
ERROR:undefined reference to `drand48(void)
drand48 is a fixed function in <stdlib>
drand48() generate random values between [0,1]
i needed values bettwen [-1,1] so i fixed it
• 04-07-2004
quzah
drand48 is not a fixed function in stdlib.h. That is to say, it's not an ANSI function. As such, depending on what compiler you happen to be using at the time, it probably doesn't exist. I suspect that's what you're running into. If it is there, make sure you're adding the .h, since you didn't mention it in your post. If it is there, then why are you prototyping it?

Quzah.
• 04-07-2004
dionys
#1okz..but prototyping that function is not an error
#2i have added .h .
#3hOW I can check that this function is in my compiler's
library
• 04-07-2004
quzah
Well if it's telling you it's not there, then obviously, it's not there. Otherwise, just check Google. That's usually what you'd do if you're trying to find some info. Or you could always look at the headers. Or the documentation for your compiler. Or man drand48, or... Like I said, Google.

Quzah.
• 04-07-2004
Thantos
If drand48() returns values between 0 and 1 then subtracting one will not solve the problem as it will give you values between -1 and 0.

Why don't you take a look at the function I gave you? It works really well and can easily be modified to not take any parameteres. And if you have to use a function called drand48() well then you can change the name.