Problem with Monte Carlo(integration by rejection)

This is a discussion on Problem with Monte Carlo(integration by rejection) within the C Programming forums, part of the General Programming Boards category; This program finds the value of π (pi) with the Monte Carlo method. #1Consiter that unit circle (radious = 1 ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    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;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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;
    }

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    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; 
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Keep all post related to the same program in the same thread:
    Problem with Monte Carlo(integration by rejection)

    2) From what you've posted you haven't actually defined drand48() anywhere.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Threads merged
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    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

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    #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

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21