Simulation Program: A circular slab of uniform thickness (5 units) (cross-sectio
ID: 3682178 • Letter: S
Question
Simulation Program: A circular slab of uniform thickness (5 units) (cross-section of the slab is illustrated below) is to be used to shield a nuclear reactor. A radioactive particle entering the shield follows a random path by moving forward, backward, left, or right with equal likelihood, in jumps of one unit (You may assume the radioactive particle begins in the shield( position zero) ). A change in the direction of the particle is interpreted as a collision with a Lead atom in the shield. After 10 collisions the particle’s energy has dissipated and will not escape the shield. If the particle moves forward 6 positions(without exceeding 10 collisions), it has escaped the shielding. If the particle returns to the reactor, it is has not escaped the shield.Write a program to simulate 1000 particles entering the shield and determine what percentage of the particles escape the shielding. Note: The simulation for each particle will end when any of the events described above has occurred.
Explanation / Answer
/****** c++ code to simulate 1000 particles entering the shield and determine what percentage of the particles escape the shielding ***/
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main (){
int particles = 1000, direction = 1, oldd, collision = 0, forward =0;
double escape=0;
const int FORWARD = 1;
const int BACKWARD = 2;
srand(time(NULL));
while (particles != 0){
oldd=1;
forward=0;
collision=0;
while( collision < 10 && (forward >= 0 && forward < 6) ){
direction = rand() % 4 + 1; //pick a number 1 through 4
if (oldd!=direction){//compare old direction to direction
collision++;
oldd = direction;
}
if (direction==FORWARD){// partical moves forward
++forward;
}
if (direction==BACKWARD){// if partical moves backward
--forward;}
}
if (forward>=6 && collision <10){
++escape;
}
particles--;// partical has died
}
cout << escape/1000 << " % of the particals escaped ";
return 0;
}