1

这里有点问题。想知道为什么我的arrivalInterval 方法的结果会得到“Infinity”。(tmp 是在上一个类中定义的,我将粘贴两个类)第一个类 RandomNum 工作得很好。

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);
}}

现在对于我遇到问题的课程。如果随机数选择 1,则除以 .1/tmp(第一类生成的随机数),如果为 2,则为 .2/tmp,如果为 3,则为 .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();
        }
    }
4

2 回答 2

1

看来tmp == 0f; Java 浮点除以零产生POSITIVE_INFINITYNEGATIVE_INFINITY取决于符号。

原因tmp0f因为你从来没有给它赋值,所以它保留了它的默认初始化值,即0f. 请注意,您从不调用ExpInterval,因此tmp在您发布的代码中确实从未修改过。

于 2012-09-17T01:04:49.090 回答
0

你从不坐tmp二等舱,因为你从不打电话ExpInterval,只是findArrivalInterval

此外,您可以int result = (Math.random()*3)+1使用set. 并将所有这些 if 语句替换为

arrivalInterval = 0.1*result/tmp
System.out.println(arrivalInterval);
return arrivalInterval;
于 2012-09-17T00:52:49.117 回答