随机数生成器的公式

时间:2012-09-17 00:47:36

标签: java random

这里有一点问题。想知道为什么我的结果为我的arrivalInterval方法得到“无限”。 (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();
        }
    }

2 个答案:

答案 0 :(得分:1)

似乎是tmp == 0f; Java浮点除零产生POSITIVE_INFINITYNEGATIVE_INFINITY,具体取决于符号。

tmp 0f的原因是因为您从未分配给它,因此它会保留其默认初始化值0f。请注意,您永远不会致电ExpInterval,因此您发布的代码中永远不会修改tmp

答案 1 :(得分:0)

您从未在第二节课中设置tmp,因为您从不致电ExpInterval,只是findArrivalInterval

此外,您可以使用int result = (Math.random()*3)+1撰写set而不是所有内容。并用

替换所有if语句
arrivalInterval = 0.1*result/tmp
System.out.println(arrivalInterval);
return arrivalInterval;