mercredi 28 octobre 2020

I can't find the problem with my particle simulation program in Java

I'm working on a project where I'm supposed to simulate the movement of a particle in the shielding of a reactor. Here is more information from the prompt.

A circular slab of uniform thickness (5 units) 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 one)). A change in the direction of the particle is interpreted as a collision with a Lead atom in the shield. For example, heading forward then right would count as a collision, while going forward then forward would not. After 10 collisions the particle’s energy has dissipated and will not escape the shield. If the particle has moved forward 6 positions (without exceeding 10 collisions), it has escaped the shielding. If the particle returns to the reactor, it has not escaped the shield. There are three possible ways a simulation can terminate for a radioactive particle: (i) The particle has moved forward 6 positions (without exceeding 10 collisions) (ii) The particle has returned back to the shield, i.e., it has moved one unit backward from its starting position (iii) Energy of the particle has been dissipated, i.e., it has collided with the lead atom 10 times Write a program to simulate 1000 particles entering the shield and determine what percentage of the particles escape the shielding. Assumptions:

  1. The particle starts in the shield wall.
  2. The initial movement of the particle is going forward.

Here's my current code. Even when I simulate 1000000000 particles and look at the number of individual escaped particles, I get 0 particles escaping each time. Is there something wrong with my RNG or my nested loops and conditionals?

public class ParticleSimulation {
    public static void main(String[] args) {
        int numParticles = 1000000000;
        int yPos = 0;
        int prevDir = 1;
        int direction;
        int numEscaped = 0;
        int numCollisions = 0;
        while (numParticles > 0) {
            while (numCollisions < 10 && yPos >= 0 && yPos < 6) {
                direction = (int)(Math.random() * 4 + 1);
                if (prevDir != direction) {
                    numCollisions++;
                    prevDir = direction;
                }
                if (direction == 1) {
                    yPos++;
                }
                if (direction == 2) {
                    yPos--;
                }
            }
            if (yPos >= 6 && numCollisions < 10) {
                numEscaped++;
            }
            numParticles--;
        }
        System.out.println(numEscaped + " particles escaped the lead shielding.");
    }
}



Aucun commentaire:

Enregistrer un commentaire