0% found this document useful (0 votes)
18 views6 pages

Monte Carlo Simulation

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Monte Carlo Simulation

Uploaded by

SJV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Monte Carlo simulation is a technique used to understand the impact of risk and uncertainty in

prediction and modeling problems. Here's a simple program in Turbo C++ to demonstrate the Monte
Carlo simulation for estimating the value of π. This program randomly generates points in a unit
square and determines how many fall inside the quarter circle to estimate π.

```cpp

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cmath>

using namespace std;

int main() {

int n;

int insideCircle = 0;

double x, y;

// Input the number of points to generate

cout << "Enter the number of points for the simulation: ";

cin >> n;

// Seed the random number generator

srand(time(0));

for (int i = 0; i < n; i++) {

// Generate random point (x, y)

x = static_cast<double>(rand()) / RAND_MAX;

y = static_cast<double>(rand()) / RAND_MAX;

// Check if the point is inside the quarter circle

if (x * x + y * y <= 1.0) {
insideCircle++;

// Estimate the value of pi

double piEstimate = 4.0 * insideCircle / n;

cout << "Estimated value of pi: " << piEstimate << endl;

return 0;

```

### Explanation:

1. **Variables**:

- `n`: Number of points to generate.

- `insideCircle`: Counter for points that fall inside the quarter circle.

- `x`, `y`: Coordinates of the generated points.

2. **Input**:

- Prompts the user to enter the number of points for the simulation.

3. **Random Number Generation**:

- Uses `srand(time(0))` to seed the random number generator with the current time to ensure
different results for each run.

- `rand()` generates random numbers. Dividing by `RAND_MAX` normalizes them to the range [0,
1].

4. **Monte Carlo Simulation**:

- For each point, generate random `x` and `y` coordinates.

- Check if the point is inside the quarter circle (`x^2 + y^2 <= 1`).

- Count the number of points inside the quarter circle.


5. **Pi Estimation**:

- The ratio of points inside the quarter circle to the total number of points, multiplied by 4,
estimates the value of π.

- Outputs the estimated value of π.

This program demonstrates the basic concept of Monte Carlo simulation by estimating the value of
π. The more points you generate, the more accurate the estimation becomes.
Here's a Turbo C++ program for Monte Carlo simulation, rewritten without using namespaces. This
example approximates the value of π using Monte Carlo methods.

```cpp

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

// Function to estimate the value of Pi using Monte Carlo simulation

double monteCarloPi(int numPoints) {

int insideCircle = 0;

for (int i = 0; i < numPoints; i++) {

double x = (double)rand() / RAND_MAX;

double y = (double)rand() / RAND_MAX;

if (x * x + y * y <= 1) {

insideCircle++;

// Calculate Pi

return (4.0 * insideCircle) / numPoints;

int main() {

int numPoints;

// Seed the random number generator

srand((unsigned)time(NULL));
cout << "Enter the number of points for the Monte Carlo simulation: ";

cin >> numPoints;

// Estimate Pi using Monte Carlo simulation

double piEstimate = monteCarloPi(numPoints);

cout << "Estimated value of Pi is: " << piEstimate << endl;

getch(); // To keep the console window open

return 0;

```

### Explanation:

1. **Monte Carlo Simulation Function (`monteCarloPi`)**:

- Takes one parameter: `numPoints` (the number of random points to generate).

- Generates random points `(x, y)` within the unit square `[0, 1] x [0, 1]`.

- Checks if the point falls within the unit circle centered at the origin by evaluating if `x^2 + y^2 <=
1`.

- Counts the number of points inside the circle and estimates π as `(4.0 * insideCircle) / numPoints`.

2. **Main Function**:

- Seeds the random number generator with the current time using `srand((unsigned)time(NULL))`.

- Prompts the user to input the number of points.

- Calls `monteCarloPi` to estimate π and prints the result.

### Notes:

- **Random Number Generation**: Uses `rand()` to generate random numbers and `srand()` to seed
the random number generator.

- **Floating-Point Arithmetic**: The division and arithmetic operations are performed using `double`
to ensure precision.
This program is compatible with Turbo C++ and provides an approximation of π using Monte Carlo
simulation. Adjust the number of points for better accuracy as needed.

You might also like