Java 程序每次应该是随机的时候都会给出相同的结果

Java 程序每次应该是随机的时候都会给出相同的结果,java,reduce,pi,Java,Reduce,Pi,这个程序的目标是取2个随机变量作为分数,看看它们是否已经简化了。假设的概率是6/(π^2)。我运行了1000个不同的变量组合,并确定了有多少是和没有减少的。然后我求π 但每次运行时,输出都会给我“pi为2.449489742783178” 有人知道为什么吗?谢谢 import java.util.*; public class ratio1 { /** * @param args */ public static void main(String[] ar

这个程序的目标是取2个随机变量作为分数,看看它们是否已经简化了。假设的概率是6/(π^2)。我运行了1000个不同的变量组合,并确定了有多少是和没有减少的。然后我求π

但每次运行时,输出都会给我“pi为2.449489742783178”

有人知道为什么吗?谢谢

import java.util.*;

public class ratio1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int nonReducedCount = 0; //counts how many non reduced ratios there are
        for(int i =1; i<=1000; i++){

            Random rand = new Random();
            int n = rand.nextInt(1000)+1;  //random int creation
            int m = rand.nextInt(1000)+1;
            //Ratio ratio = new Ratio(n,m);
            if (gcd(n,m)> 1 ){ // if the ratio was not already fully reduced
                nonReducedCount++; // increase the count of non reduced ratios
            }   
        }

        int reducedCount = 1000 - nonReducedCount; //number of times the ratio was reduced already
        double reducedRatio = reducedCount / nonReducedCount; //the ratio for reduced and not reduced
        reducedRatio *= 6;
        reducedRatio = Math.sqrt(reducedRatio);
        System.out.println("pi is " + reducedRatio);
    }

    public static int gcd(int a, int b) { return b==0 ? a : gcd(b,a%b); }

}
import java.util.*;
公共类比率1{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
int nonReducedCount=0;//计算有多少个未减少的比率
对于(inti=1;i1){//如果比率尚未完全降低
nonReducedCount++;//增加未减少比率的计数
}   
}
int reducedCount=1000-nonReducedCount;//比率已减少的次数
double reducedRatio=reducedCount/nonReducedCount;//reduced与not reduced的比率
还原率*=6;
reducedRatio=Math.sqrt(reducedRatio);
System.out.println(“pi为”+简化比率);
}
公共静态intgcd(inta,intb){返回b==0?a:gcd(b,a%b);}
}

当您对两个整数进行除法时,您将得到整数除法,并得到一个整数结果,即使您稍后将结果分配给一个
双精度
。试一试

double reducedRatio = (double)reducedCount / nonReducedCount;

i、 e.将其中一个操作数转换为
double

当您将两个整数除法时,您将得到整数除法,并得到一个整数结果,即使您稍后将结果分配给一个
double
。试一试

double reducedRatio = (double)reducedCount / nonReducedCount;
i、 e.将其中一个操作数转换为
double