在Java中怎么求方差和标准差

如题所述

Java求方差和标准差:
public class GetAverageandStandardDevition {

private int[] array = new int[10];
private int num = 10;

public int getRandomDigit() {
return (int) (Math.random() * 1000);
}

public void getTargetDigit() {

for (int i = 0; i < num; i++) {
array[i] = getRandomDigit();
System.out.println(array[i]);
}
}

//方差
public double getAverage(){
int sum = 0;
for(int i = 0;i < num;i++){
sum += array[i];
}
return (double)(sum / num);
}

//标准差
public double getStandardDevition(){
double sum = 0;
for(int i = 0;i < num;i++){
sum += Math.sqrt(((double)array[i] -getAverage()) * (array[i] -getAverage()));
}
return (sum / (num - 1));
}

public static void main(String[] args) {

GetAverageandStandardDevition gcs = new GetAverageandStandardDevition();

gcs.getTargetDigit();
System.out.println(gcs.getAverage() + " " + gcs.getStandardDevition());

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答