Link to home
Start Free TrialLog in
Avatar of cainsaw
cainsaw

asked on

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.io.*;
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;
          Math.random();
          Time = 1 + (int)(Math.random() * 10);
          return Time;
      }//end TimeUnit
 
}//end of RandNumber
ASKER CERTIFIED SOLUTION
Avatar of bcladd
bcladd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial