Reputation: 103
Having a bit of an issue here. Wondering why I am getting "Infinity" for my result for my arrivalInterval method. (tmp is defined in a previous class, I will paste both classes) The first class, RandomNum, works just fine.
import java.util.Random;
public class RandomNum {
static double x = 0.1;
static double y = 0.2;
static double z = 0.3;
static double lamda;
static double tmp;
static double count1, count2, count3;
static double average;
static Random randomNumbGen = new Random();
// 3 methods for testing x, y, and z
public static double ExpInterval(double lamda)
{
tmp=randomNumbGen.nextDouble();
return (-(1/lamda) * Math.log(tmp));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// loop that runs then methods then adds them to the count (for an an average later) and prints out the results
for (int i=1; i<1001; i++)
count1+=ExpInterval(x);
System.out.println(ExpInterval(x));
count2+=ExpInterval(y);
System.out.println(" " + ExpInterval(y));
count3+=ExpInterval(z);
System.out.println(" " + ExpInterval(z));
}
//calculates average then prints it out
average = count1/1000;
System.out.println("The Average of the first variable is" + average +" " + "1/lamba is " + 1/x);
average = count2/1000;
System.out.println("The average of the second variable is " + average + " " + "1/lamba is " + 1/y);
average = count3/1000;
System.out.println("The average of the third variable is " + average + " " + "1/lamba is " + 1/z);
}}
now for the class I am having the issue with. If the random number picks 1, then divide .1/tmp(random number generated in first class), if 2, then .2/tmp, and if 3, then .3/tmp
import java.util.*;
public class RandomProcess extends RandomNum
{
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
public double findArrivalInterval()
{
double arrivalInterval = 0.0;
set.add(1);
set.add(2);
set.add(3);
Integer result = (Integer) set.toArray()[random.nextInt(set.size())]; //only 1, 2, or 3 to be picked
if (result == 1)
{
arrivalInterval = .1/tmp;
System.out.println(arrivalInterval);
return arrivalInterval;
}
if (result == 2)
{
arrivalInterval = .2/tmp;
System.out.println(arrivalInterval);
return arrivalInterval;
}
if (result == 3)
{
arrivalInterval = .3/tmp;
System.out.println(arrivalInterval);
return arrivalInterval;
}
System.out.print(arrivalInterval);
return arrivalInterval;
}
public static void main(String[] args)
{
RandomProcess testProcess = new RandomProcess();
testProcess.findArrivalInterval();
}
}
Upvotes: 1
Views: 504
Reputation: 29636
It seems tmp == 0f
; Java floating-point division-by-zero produces either POSITIVE_INFINITY
or NEGATIVE_INFINITY
depending on the sign.
The reason tmp
is 0f
is because you never assign to it, so it keeps its default initialization value, which is 0f
. Note that you never call ExpInterval
, so indeed tmp
is never modified in the code you posted.
Upvotes: 1
Reputation: 1058
You never set tmp
in the second class because you never call ExpInterval
, just findArrivalInterval
.
Also, you can write int result = (Math.random()*3)+1
instead of all that stuff with set
. And replace all those if statements with
arrivalInterval = 0.1*result/tmp
System.out.println(arrivalInterval);
return arrivalInterval;
Upvotes: 0