Monte Carlo simula on is a technique used to understand the impact of risk and uncertainty in
predic on and modeling problems. Here's a simple program in Turbo C++ to demonstrate the Monte
Carlo simula on for es ma ng the value of π. This program randomly generates points in a unit
square and determines how many fall inside the quarter circle to es mate π.
```cpp
#include <iostream>
#include <cstdlib>
#include <c me>
#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 simula on: ";
cin >> n;
// Seed the random number generator
srand( me(0));
for (int i = 0; i < n; i++) {
// Generate random point (x, y)
x = sta c_cast<double>(rand()) / RAND_MAX;
y = sta c_cast<double>(rand()) / RAND_MAX;
// Check if the point is inside the quarter circle
if (x * x + y * y <= 1.0) {
insideCircle++;
// Es mate the value of pi
double piEs mate = 4.0 * insideCircle / n;
cout << "Es mated value of pi: " << piEs mate << endl;
return 0;
```
### Explana on:
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 simula on.
3. **Random Number Genera on**:
- Uses `srand( me(0))` to seed the random number generator with the current me 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 Simula on**:
- 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 Es ma on**:
- The ra o of points inside the quarter circle to the total number of points, mul plied by 4,
es mates the value of π.
- Outputs the es mated value of π.
This program demonstrates the basic concept of Monte Carlo simula on by es ma ng the value of
π. The more points you generate, the more accurate the es ma on becomes.
Here's a Turbo C++ program for Monte Carlo simula on, rewri en without using namespaces. This
example approximates the value of π using Monte Carlo methods.
```cpp
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include < me.h>
// Func on to es mate the value of Pi using Monte Carlo simula on
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) me(NULL));
cout << "Enter the number of points for the Monte Carlo simula on: ";
cin >> numPoints;
// Es mate Pi using Monte Carlo simula on
double piEs mate = monteCarloPi(numPoints);
cout << "Es mated value of Pi is: " << piEs mate << endl;
getch(); // To keep the console window open
return 0;
```
### Explana on:
1. **Monte Carlo Simula on Func on (`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 evalua ng if `x^2 + y^2 <=
1`.
- Counts the number of points inside the circle and es mates π as `(4.0 * insideCircle) / numPoints`.
2. **Main Func on**:
- Seeds the random number generator with the current me using `srand((unsigned) me(NULL))`.
- Prompts the user to input the number of points.
- Calls `monteCarloPi` to es mate π and prints the result.
### Notes:
- **Random Number Genera on**: Uses `rand()` to generate random numbers and `srand()` to seed
the random number generator.
- **Floa ng-Point Arithme c**: The division and arithme c opera ons are performed using `double`
to ensure precision.
This program is compa ble with Turbo C++ and provides an approxima on of π using Monte Carlo
simula on. Adjust the number of points for be er accuracy as needed.