Program to define simulation method, C/C++ Programming

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of π using a simulation method called "Monte Carlo". The following figure shows a circle of radius 1, centered at the origin and circumscribed by a square.

1937_Program to define simulation method2.png

Imagine that this is a dartboard and that you are tossing darts at it randomly. With enough darts, the ratio of darts in the circle (Nc) to total darts thrown (Nt) is approximately equal the ratio between the area of the circle (circle_area = π(1)2) and the area of the square (square_area = 4) i.e.

2360_Program to define simulation method.png

Therefore, 4Nc/Nt . We can simplify the math by only considering the first quadrant, calculating the ratio of the number of darts landed on the first quadrant over the number of darts landed on the top right square's area.

Study the following link to learn how to assign a random number to a variable. Define variables to store the x and y coordinates of a particular dart throw. In other words a random number between [0,1] gets assigned to the variables x and y, each (simulating one dart throw). Now, place your x and y declarations in a For-loop to simulate N dart throws. Note that all of these darts land on the top right square.

Use the Euclidean distance formula,2275_Program to define simulation method1.png to check if a dart is landed inside the first quadrant. Maintain account of the number of darts that end up in the first quadrant of the unit circle.

Now use your loop to build a π-calculating function. The function should take one argument specifying the number of dart throws to run. It should return the decimal value of π, using the technique outlined above. You should get pretty good results for around 5,000,000 dart throws! Isn't this neat?

Posted Date: 2/14/2013 4:41:55 AM | Location : United States







Related Discussions:- Program to define simulation method, Assignment Help, Ask Question on Program to define simulation method, Get Answer, Expert's Help, Program to define simulation method Discussions

Write discussion on Program to define simulation method
Your posts are moderated
Related Questions
Change the matrix program (program 3) slightly. Overload == operator to compare two matrices to be added or subtracted. i.e., whether the column of first and the row of second

Define External Storage Class - computer programming? When a variable is declared outside a function the storage is permanently assigned to it and its storage class is 'extern'

How do I allocate multidimensional arrays by new? A: There are several ways to do this, based on how flexible you wish the array sizing to be. On one acute, if you know all the

What does ODBC do in context with PHP? PHP supports many databases such as dBase, Microsoft SQL Server, Oracle, etc. however, it also supports databases such as filePro, FrontB

Converting Base Type to Class Type (char to string)   class String { char *name; int length; public: String(void) {length =0; name = new char[length+1];

C program for function  of average int average(int); void main() {           int max=0,c=0;           clrscr();           printf("ENTER THE LIMIT OF INPUT FOR AV

Describe about the container class. -Class to hold objects in external storage or memory. It acts as a generic holder. - It has a predefined behaviour and a known interface.

Project Description: Seeking a Voting Bot Script / Program Skills required are ASP, Javascript, C Programming, PHP, Software Architecture

Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements

What happens while a derived-class object is developed & destroyed? A: Space is allocated (on the heap or the stack) for the full object (i.e. adequate space to store the data m