JAVA - Adding & Averaging Random Numbers

The newbie is back.  I am still in the process of writing my “larger program” (ultimately, I hope too simulate a Scan of a disk).  NOTE:  I am writing in stages so I don’t have a huge mess of code to try to debug (been there, done that and I did not enjoy it).  What is wrong with my logic?  I thought that when one wrote a program that it went from top to bottom in its processing.  That said, in my program, I am attempting to accumulate the Time Units (see code).  Eventually, I hope to get an average time.  My logic is that I have initialized prevNum to zero (0).  Then I add it (prevNum) to T_Unit.TimeUnit() in an attempt to get an accumulated time (AccumTime).  In the next line, I make prevNum = to T_Unit.TimeUnit() in the hopes that the next time through the loop I get a new AccumTime.  I don’t think T_Unit.TimeUnit() is staying the same in both lines of code.  Is it possible to make them the same?

As you can see in the sample output, prevNum is not “growing” as I would hope.  It is actually smaller.

C:\forte4j\Assignment 3>java RandNumber
AccumTime is 1.0
Avgwait is 1.0
PrevNum is 8.0
Time Unit is 7

AccumTime is 16.0
Avgwait is 8.0
PrevNum is 1.0
Time Unit is 1

The code:

import java.util.*;

public class RandNumber {
      public RandNumber () {}

      public static void main (String args [])
          RandNumber T_Unit = new RandNumber ();

          double count = 0;
          double prevNum = 0;
          double AvgWait = 0;
          double AccumTime = 0;

          while (count < 2)
            AccumTime = prevNum + T_Unit.TimeUnit();
            prevNum = T_Unit.TimeUnit();
            count ++;
            AvgWait = AccumTime/count;

            //System.out.println("Count is " +count);
            System.out.println("AccumTime is " +AccumTime);            
            System.out.println("Avgwait is " +AvgWait);
            System.out.println("PrevNum is " +prevNum);
            System.out.println("Time Unit is " +T_Unit.TimeUnit() + "\n");

          }//end while loop
      }//end of main

      public static int TimeUnit ()
          int Time;
          Time = 1 + (int)(Math.random() * 10);
          return Time;
      }//end TimeUnit
}//end of RandNumber
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

(1) AcumTime is the value you want to initialize to 0 and then add to. That is:

  prevNum = T_Unit.TimeUnit(); // this records a randomized value (okay, the scan time but for now it is random, right?)
  AccumTime = AccumTime + prevNum; // DON'T call TimeUnit() again as it will get a different value (with high probability).

You also probably don't want to calculate the average until after the loop finishes but that is not really very important.

Alternatively (and equivalently) you can write the AccumTime line above as

  AccumTime += prevNum;

Hope this helps,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.