Thread: finding area of unit circle by monte carlo..

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    finding area of unit circle by monte carlo..

    Hi guys, I am attempting this, but cannot put finger on what is wrong. I have done it assuming a unit circle on origin and I thus use the positive quadrant and multiply the answer by 4.


    Think maybe to do with the way I have generated random numbers.

    Many thanks for your ideas..


    insert
    Code:
    #include<stdlib.h>  
    #include<stdio.h>  
    #include<math.h>  
    
    
    
    int main()
    
    {
        
    int N=100000,j;
    double x1,x2,hit;
    
      
       x1=rand()/(double)RAND_MAX; //insuring random numbers between 0,1
       x2=rand()/(double)RAND_MAX;
       
       for(j=0;j<N;j++)   
       
           if(x1*x1+x2*x2<=1)
           
               {     
               hit++;
               }
        
        printf("volume: &#37;f", (4.0*hit)/N);  
        
    
        
    
    system("pause");
    exit(0);
    }
    Last edited by niceguy; 02-24-2008 at 02:21 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You aren't generating the random numbers in your loop...
    Oh you should probably initialize hit too...
    Last edited by arpsmack; 02-24-2008 at 02:26 PM.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Quote Originally Posted by arpsmack View Post
    You aren't generating the random numbers in your loop...
    Oh you should probably initialize hit too...
    Ok i think i made the modifications...but still not right.


    include
    Code:
    #include<stdio.h> 
    #include<stdlib.h>  
    #include<math.h>  
    #include<time.h>  
    
    int main()
    
    {
        
    int N=100000,j;
    double x1,x2,hit=0;
    
       srand(time(NULL));
    
      
       x1=rand()/(double)RAND_MAX;
       x2=rand()/(double)RAND_MAX;
       
       for(j=0;j<N;j++)   
       
           if(x1*x1+x2*x2<=1)
           
               {     
               hit++;
               }
        
        printf("volume: &#37;f\n", (4.0*hit)/N);  
        
    system("pause");
    exit(0);
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(j=0;j<N;j++)   
    {
       x1=rand()/(double)RAND_MAX;
       x2=rand()/(double)RAND_MAX;
       if(x1*x1+x2*x2<=1)
       {     
               hit++;
       }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    With Vart's code you may want to change your equation to:

    printf("Area of circle: &#37;f\n", 4.0 * (hit/N));

    Guessing you are needing area and not volume? Area = r^2 *PI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  2. newbie here.. pls help me in this program!
    By rothj0hn in forum C Programming
    Replies: 2
    Last Post: 02-01-2006, 10:40 PM
  3. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 12:50 PM
  4. Finding a point within a circle
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2001, 08:34 PM
  5. monte carlo methods
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2001, 11:29 PM