Monte Carlo Lab
Monte Carlo Lab
MONTE-CARLO LAB
Name: Raja Vignesh Selvamani
Student Id: 202103607
int main() {
const int n = 100000;
const int nsteps = 10;
const double pi = 3.1415926;
int itotal[nsteps + 1] = {0};
srand(time(NULL));
for (int i = 0; i < n; ++i) {
double x = 0.0;
double y = 0.0;
for (int istep = 0; istep < nsteps; ++istep) {
double angle = 2.0 * pi * rand() / RAND_MAX;
x += cos(angle);
y += sin(angle);
}
int itemp = static_cast<int>(sqrt(x * x + y * y));
if (itemp <= nsteps) {
itotal[itemp]++;
}
}
for (int i = 0; i <= nsteps; ++i) {
std::cout << "Distance " << i << ": " << itotal[i] << "
particles" << std::endl;
}
return 0;
}
Output:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mpi.h>
Output: