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_INFINITYまたはを生成します。NEGATIVE_INFINITY

その理由tmp0f、決して割り当てないため、デフォルトの初期化値である0f. を呼び出さないことに注意してください。ExpInterval実際、tmp投稿したコードで変更されることはありません。

于 2012-09-17T01:04:49.090 に答える
0

tmpを呼び出さないため、2 番目のクラスに設定することはありませExpIntervalfindArrivalInterval

また、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 に答える