finding area of unit circle by monte carlo..

This is a discussion on finding area of unit circle by monte carlo.. within the C Programming forums, part of the General Programming Boards category; Hi guys, I am attempting this, but cannot put finger on what is wrong. I have done it assuming a ...

  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 01:21 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    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 01: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    for(j=0;j<N;j++)   
    {
       x1=rand()/(double)RAND_MAX;
       x2=rand()/(double)RAND_MAX;
       if(x1*x1+x2*x2<=1)
       {     
               hit++;
       }
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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, 09:40 PM
  3. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 11:50 AM
  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

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