[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

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
0
cainsaw
Asked:
cainsaw
1 Solution
 
bcladdCommented:
(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).
  count++;

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,
-bcl
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now