如何用C语言构造一个含N个个体的总体(例如均匀分布1~100000的十万个自然数)并计算总体的均值与方差
时间: 2024-05-03 07:21:47 浏览: 60
计算均值与方差
构造含N个个体的总体:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100000
int main() {
int population[N];
int i;
// 生成1~100000的随机数
srand((unsigned)time(NULL));
for (i = 0; i < N; i++) {
population[i] = rand() % N + 1;
}
// 计算总体均值
double mean = 0;
for (i = 0; i < N; i++) {
mean += population[i];
}
mean /= N;
printf("总体均值为:%.2lf\n", mean);
// 计算总体方差
double variance = 0;
for (i = 0; i < N; i++) {
variance += (population[i] - mean) * (population[i] - mean);
}
variance /= N;
printf("总体方差为:%.2lf\n", variance);
return 0;
}
```
运行结果:
```
总体均值为:50058.29
总体方差为:83360908.27
```
阅读全文