Solved

# JAVA - Adding & Averaging Random Numbers

Posted on 2004-11-27
417 Views
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
Question by:cainsaw
1 Comment

LVL 11

Accepted Solution

(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

### Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…