I need to use c++ and monte carlo random number generator to solve this problem.
ID: 3716045 • Letter: I
Question
I need to use c++ and monte carlo random number generator to solve this problem.
My current code is this, but it isnt working:
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
double random;
double c =0;
double x = 0;
double y = 0;
double z = 0;
double x2 = 0;
double y2 = 0;
double z2 = 0;
double stepsize;
double stepsize2;
int counter = 0;
for (int i = 0; i < 50; i++)
{
double c;
double currentX = -1;
double currentX2 = 1;
double currentY = 0;
double currentY2 = 0;
double currentZ = 0;
double currentZ2 =0;
while(currentX*currentX + currentY*currentY+ currentZ*currentZ <= 100 && c !=1)
{
//cout << currentX << " " << currentX2 << " " << currentY << " " << currentY2 << " " << currentZ << " " << currentZ2 << endl;
do {
x = double(rand())/RAND_MAX*2-1;
y = double(rand())/RAND_MAX*2-1;
z = double(rand())/RAND_MAX*2-1;
}
while(x*x + y*y + z*z > 1);
{
stepsize = double(rand())/RAND_MAX;
double magnitude=sqrt(x*x + y*y + z*z);
x /= magnitude;
y /= magnitude;
z /= magnitude;
currentX += x*stepsize;
currentY += y*stepsize;
currentZ += z*stepsize;
}
do {
x2 = double(rand())/RAND_MAX*2-1;
y2 = double(rand())/RAND_MAX*2-1;
z2 = double(rand())/RAND_MAX*2-1;
}
while(x2*x2 + y2*y2 + z2*z2 > 1);
stepsize2 = double(rand())/RAND_MAX;
double magnitude=sqrt(x*x + y*y + z*z);
x2 /= magnitude;
y2 /= magnitude;
z2 /= magnitude;
currentX2 += x2*stepsize2;
currentY2 += y2*stepsize2;
currentZ2 += z2*stepsize2;
}
if(sqrt((currentX-currentX2)*(currentX-currentX2) + (currentY-currentY2)*(currentY-currentY2) + (currentZ-currentZ2)*(currentZ-currentZ2) < 1));
{
c==1;
counter++;
}
//cout << stepsize << " " << stepsize2 << endl;
//cout << currentX << " " << currentX2 << " " << currentY << " " << currentY2 << " " << currentZ << " " << currentZ2 << endl;
//cout << sqrt((currentX-currentX2)*(currentX-currentX2) + (currentY-currentY2)*(currentY-currentY2) + (currentZ - currentZ2)*(currentZ - currentZ2)) << endl;
}
cout << counter/500 <<endl;
}
4. An exotic particle, the bobbyon, and its accompanying anti-particle, the anti-bobbyon, are located in a sphere with a radius of 10 mm. The bobbyon is initially located at (-1, 0,0)mm and the anti- bobbyon is located at (1, 0,0)mm if the two particles come within a radius of 1mm of one another they will annihilate. Calculate the probability that the two particles will annihilate instead of leaving the sphere (average over about 1x10 runs), assume the particles to be points. Both particles have the same distribution of step size which varies uniformly between 0 and 1mm and at each step the particles scatters in a random direction isotropically.Explanation / Answer
Its working without any error.
Please check with your C++ version.
*****************************************************
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
double random;
double c =0;
double x = 0;
double y = 0;
double z = 0;
double x2 = 0;
double y2 = 0;
double z2 = 0;
double stepsize;
double stepsize2;
int counter = 0;
for (int i = 0; i < 50; i++)
{
double c;
double currentX = -1;
double currentX2 = 1;
double currentY = 0;
double currentY2 = 0;
double currentZ = 0;
double currentZ2 =0;
while(currentX*currentX + currentY*currentY+ currentZ*currentZ <= 100 && c !=1)
{
//cout << currentX << " " << currentX2 << " " << currentY << " " << currentY2 << " " << currentZ << " " << currentZ2 << endl;
do {
x = double(rand())/RAND_MAX*2-1;
y = double(rand())/RAND_MAX*2-1;
z = double(rand())/RAND_MAX*2-1;
}
while(x*x + y*y + z*z > 1);
{
stepsize = double(rand())/RAND_MAX;
double magnitude=sqrt(x*x + y*y + z*z);
x /= magnitude;
y /= magnitude;
z /= magnitude;
currentX += x*stepsize;
currentY += y*stepsize;
currentZ += z*stepsize;
}
do {
x2 = double(rand())/RAND_MAX*2-1;
y2 = double(rand())/RAND_MAX*2-1;
z2 = double(rand())/RAND_MAX*2-1;
}
while(x2*x2 + y2*y2 + z2*z2 > 1);
stepsize2 = double(rand())/RAND_MAX;
double magnitude=sqrt(x*x + y*y + z*z);
x2 /= magnitude;
y2 /= magnitude;
z2 /= magnitude;
currentX2 += x2*stepsize2;
currentY2 += y2*stepsize2;
currentZ2 += z2*stepsize2;
}
if(sqrt((currentX-currentX2)*(currentX-currentX2) + (currentY-currentY2)*(currentY-currentY2) + (currentZ-currentZ2)*(currentZ-currentZ2) < 1));
{
c==1;
counter++;
}
//cout << stepsize << " " << stepsize2 << endl;
//cout << currentX << " " << currentX2 << " " << currentY << " " << currentY2 << " " << currentZ << " " << currentZ2 << endl;
//cout << sqrt((currentX-currentX2)*(currentX-currentX2) + (currentY-currentY2)*(currentY-currentY2) + (currentZ - currentZ2)*(currentZ - currentZ2)) << endl;
}
cout << counter/500 <<endl;
}