Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何得到随机数来测试数学方程?_Java_Math_Random - Fatal编程技术网

Java 如何得到随机数来测试数学方程?

Java 如何得到随机数来测试数学方程?,java,math,random,Java,Math,Random,我对这个方程有困难:E*余切((q*E)/(k*t))=nkT/q 我在寻找E的值,因为可以实现相等 知道:q和k是常数,t和n是变量。 为此,我尝试了此代码,但显然失败了: public class Equation { public static double q = 1.6E-19; public static double k = 1.38E-23; //double y = E * 1/Math.tan(E*q/k*t); //DecimalForma

我对这个方程有困难:
E*余切((q*E)/(k*t))=nkT/q

我在寻找E的值,因为可以实现相等 知道:qk是常数,tn是变量。
为此,我尝试了此代码,但显然失败了:

public class Equation {
    public static double q = 1.6E-19;
    public static double k = 1.38E-23;

    //double y = E * 1/Math.tan(E*q/k*t);
    //DecimalFormat df = new DecimalFormat("#.#####");
    public static double[] nktq = new double[]{0.02857,0.02674,0.03118,0.02829,0.02976,0.02898,0.03001,0.02953,0.032};
    public static double[] t = new double[]{80,100,150,200,250,280,300,320,350};
    public static double[] n = new double[]{4.14,3.1,2.41,1.64,1.38,1.20,1.16,1.07,1.06};
    private static DecimalFormat df2 = new DecimalFormat(".##");

    public static double genE(){
       double start = 0;
       double end = 30;
       double random = new Random().nextDouble();
       // DecimalFormat df = new DecimalFormat("#.##");
       // df.setRoundingMode(RoundingMode.FLOOR);
        double E = start + (random * (end - start));

        //double E = new Double(df.format(result));
        System.out.println(df2.format(E));
    return E;
    }



    public static void main(String[] args) {
        //double y = E * 1/Math.tan(E*q/k*t);
        //DecimalFormat df = new DecimalFormat("#.#####")
        double E = 0;
        while ( Math.round(E *  1/Math.tan((q * E)/(k * t[0]))*100000)!= nktq[0]){
             genE();
        }
    } 
}

感谢您的帮助

为每个值创建一个新的
随机
实例。那是错误的

以下是我建议您尝试的:

public class Equation {
    private Random random;

    public Equation() { this(null); }

    public Equation(Long seed) {
        this.random = (seed == null) ? new Random() : new Random(seed.longValue());
    }

    public double nextConst() { return this.random.nextDouble(); }

    public double nextConst(double start, double end) {
        return start + (end-start)*this.random.nextDouble();
    }

    // Add the rest of your stuff here.
}

请更具体地说明它“显然失败了”的原因。