要求:
1、编写程序,实现如下功能。
(1)随机生成 1000000 个 0~1 之间的数;
(2)统计分析这些数据,计算均值、方差和分布情况,分布情况按
0.01 的步长进行统计;
(3)使用内存映射,将这些数及计算的均值、方差及分布情况写入
文件,同时将结果在终端显示;
随机数的存放要求使用动态内存分配的方式,注意内存的释放。
代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>#define NUM_SAMPLES 1000000
#define STEP_SIZE 0.01
#define NUM_BINS (int)(1.0 / STEP_SIZE)double calculate_mean(double *data, int size) {//计算数组的平均值double sum = 0.0;for (int i = 0; i < size; i++) {sum += data[i];}return sum / size;
}double calculate_variance(double *data, int size, double mean) {//计算数组的方差double sum = 0.0;for (int i = 0; i < size; i++) {sum += (data[i] - mean) * (data[i] - mean);}return sum / size;
}void calculate_distribution(double *data, int size, int *distribution) {//根据步长统计数组的分布情况for (int i = 0; i < size; i++) {int bin = (int)(data[i] / STEP_SIZE);if (bin < NUM_BINS) {distribution[bin]++;}}
}int main() {srand(time(NULL));double *data = (double *)malloc(NUM_SAMPLES * sizeof(double));if (data == NULL) {perror("Failed to allocate memory for data");exit(EXIT_FAILURE);}for (int i = 0; i < NUM_SAMPLES; i++) {data[i] = ((double)rand() / RAND_MAX);}double mean = calculate_mean(data, NUM_SAMPLES);double variance = calculate_variance(data, NUM_SAMPLES, mean);int *distribution = (int *)calloc(NUM_BINS, sizeof(int));if (distribution == NULL) {perror("Failed to allocate memory for distribution");free(data);exit(EXIT_FAILURE);}calculate_distribution(data, NUM_SAMPLES, distribution);printf("Mean: %lf\n", mean);printf("Variance: %lf\n", variance);printf("Distribution:\n");for (int i = 0; i < NUM_BINS; i++) {printf("[%lf, %lf): %d\n", i * STEP_SIZE, (i + 1) * STEP_SIZE, distribution[i]);}int fd = open("output.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);if (fd == -1) {perror("Failed to open file for memory mapping");free(data);free(distribution);exit(EXIT_FAILURE);}off_t file_size = (NUM_SAMPLES * sizeof(double)) + (NUM_BINS * sizeof(int)) + (2 * sizeof(double)); // Data + Distribution + Mean + Varianceif (ftruncate(fd, file_size) == -1) {perror("Failed to resize file");close(fd);free(data);free(distribution);exit(EXIT_FAILURE);}void *map = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);if (map == MAP_FAILED) {perror("Failed to memory map file");close(fd);free(data);free(distribution);exit(EXIT_FAILURE);}double *mapped_data = (double *)map;int *mapped_distribution = (int *)((char *)map + (NUM_SAMPLES * sizeof(double)));double *mapped_mean = (double *)((char *)map + (NUM_SAMPLES * sizeof(double)) + (NUM_BINS * sizeof(int)));double *mapped_variance = mapped_mean + 1;memcpy(mapped_data, data, NUM_SAMPLES * sizeof(double));memcpy(mapped_distribution, distribution, NUM_BINS * sizeof(int));*mapped_mean = mean;*mapped_variance = variance;close(fd);munmap(map, file_size);free(data);free(distribution);return 0;
}
运行结果:
创建并保存程序文件
使用gcc编译程序生成可执行文件:在使用gcc编译的过程中我遇到了无法编译的情况,gcc 没有使用 C99 标准来编译代码,而是使用了更旧的 C 标准,在其中添加-std解决了问题。
运行该程序得出结果:多次运行后产生的随机数服从均匀分布。