简体   繁体   中英

having trouble understanding count and average

Description: I have to create a program that simulates a man standing on a seven foot bridge, while the man stands in the middle(3.5 ft). I have to use a random number simulator and puts on either 0 or 1. 1 representing one step forward, while 0 represents one step backwards. when the man steps off the bridge (less than 0 or greater than 7) then it restarts. I am supposed to run this 1000 times(not shown in output). Then, I have to calculate the avg number of steps he takes before walking off. Also, I have to calculate the greatest number of steps he took in the 1000 runs. So far, I am having trouble creating a count, making an average, and how to calculate the greatest number of steps. Can anyone help me understand a better way to look at my coding for doing all of this. Any help would be appreciated.

import java.util.*;
public class Prog214a
{
   public static void main(String[] args)
   {
       int greatesNumOfSteps;
       double bridge,sum,avg;
       int beginning = 1;
       double midway = 3.5;

       System.out.println("Run     Average     Greatest Number of Steps");

       for(beginning = 1; beginning <= 1000; beginning++)
       {
           Random randomNum = new Random();
           int step = randomNum.nextInt(2);
           if(step == 1)
           {
               midway = midway + 1;
               sum = midway;

           }
           else
           {
               midway = midway - 1;
               sum = midway;
           }
           if(midway < 0.5 || midway > 6.5)
           {
              System.out.println("#" + beginning + "     ...");
              midway = 3.5;
           }
       }
   }
}

/**My Output:

Run     Average     Greatest Number of Steps
#10     ...
#34     ...
#46     ...
#58     ...
#122     ...
#132     ...
#148     ...
#160     ...
#188     ...
#218     ...
#228     ...
#266     ...
#312     ...
#322     ...
#338     ...
#344     ...
#376     ...
#394     ...
#398     ...
#408     ...
#418     ...
#426     ...
#438     ...
#444     ...
#452     ...
#470     ...
#480     ...
#526     ...
#542     ...
#546     ...
#562     ...
#576     ...
#584     ...
#616     ...
#662     ...
#668     ...
#704     ...
#726     ...
#732     ...
#740     ...
#750     ...
#792     ...
#798     ...
#804     ...
#814     ...
#818     ...
#830     ...
#840     ...
#844     ...
#850     ...
#864     ...
#874     ...
#884     ...
#900     ...
#914     ...
#918     ...
#928     ...
#968     ...
#978     ...
*/

This is what is supposed to look like:

Runs Average Greatest Number of Steps

1 xx.xx xx

You should boil this down into many different sub-problems:

  1. You need to run a test 1000 times
  2. For each execution, you need to run the simulation of how many steps it takes the man to go off of the bridge
  3. While the man is not off the bridge, you need to randomly see which direction his next step will be and take it
  4. Keep track of how many steps he takes total
  5. When he walks off the bridge, compare the amount of steps this execution took compared to all other executions
  6. Keep track of how many steps total there has been across all executions so you can compute the average

From the description, it doesn't sound like you need to output anything for each run, only after each execution. Here is a little skeleton based off of your file (I hope I'm not giving away too much) that should show you a better structure.

import java.util.*;

public class Prog214a {
    // Bridge is 7 feet
    private static final float BRIDGE_LENGTH = 7.0f;
    // Man starts at 3.5 ft
    private static final float MAN_START = 3.5f;
    // [1] Times to run the test
    private static final int TOTAL_ITERATIONS = 1000;

    public static void main(String[] args) {

        // [5] Initialize a max low so that first iteration will always be greater
        int greatestNumOfSteps = Integer.MIN_VALUE;
        // [6] Initialize the total number of steps over all executions
        int totalSteps = 0;

        // [2] Generate a random number for this program execution
        final Random randomNum = new Random();

        System.out.println("Average\tGreatest Number of Steps");

        for (int iteration = 1; iteration <= TOTAL_ITERATIONS; iteration++) {
            // [2] Initialize this current man's starting position
            float currentManPosition = MAN_START;
            // [4] Initialize the amount of steps the man has taken this time
            int stepsTaken = 0;
            while (// [3] condition to check both sides of the bridge) {
                // [3] Use a boolean to determine if he should step forward or not since those are the only 2 possibilities (can use int if needed)
                boolean shouldStepForward = randomNum.nextBoolean();
                if (shouldStepForward) {
                    currentManPosition = currentManPosition + 1;
                } else {
                    currentManPosition = currentManPosition - 1;
                }
                // [4] what would do to keep track of the steps for this iteration?
            }
            if (// [5] Check if this runs steps were bigger than the greatest so far) {
            }
            // [6] Add steps to the total
            totalSteps += stepsTaken;
        }
        // [6] Compute the average of the total steps
        System.out.printf("%.2f\t%d\n", averageSteps, greatestNumOfSteps);
    }
}

When I ran mine the average number of steps was usually around 16, and the max number of steps ran usually in the 80-90 range, but was varied.

One way to organize computer programs would be to use Petri's Net Elements. The organization of computer programs in terms of these net elements is also called a Petri Net.

The following are images were derived from a PDF file I created. The PDF version includes a JavaScript program that runs an interactive simulation.

在此处输入图片说明在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM