c++ - Usage of arrays to generate random numbers
问题描述
I'm trying to generate N random floats between 0 and 1 where N is specified by the user. Then I need to find the mean and the variance of the generated numbers. Struggling with finding the variance.
Already tried using variables instead of an array but have changed my code to allow for arrays instead.
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main(){
int N, i;
float random_numbers[i], sum, mean, variance, r;
cout << "Enter an N value" << endl;
cin >> N;
sum = 0;
variance = 0;
for (i = 0; i < N; i++) {
srand(i + 1);
random_numbers[i] = ((float) rand() / float(RAND_MAX));
sum += random_numbers[i];
cout << random_numbers[i] << endl;
mean= sum / N;
variance += pow(random_numbers[i]-mean,2);
}
variance = variance / N;
cout << " The sum of random numbers is " << sum << endl;
cout << " The mean is " << mean << endl;
cout << " The variance is " << variance << endl;
}
The mean and sum is currently correct however the variance is not.
解决方案
The mean
you calculate inside the loop is a "running-mean", ie for each new incoming number you calculate the mean up to this point. For the variance however your forumla is incorrect. This:
variance += pow(random_numbers[i]-mean,2);
would be correct if mean
was the final value, but as it is the running mean the result for variance is incorrect. You basically have two options. Either you use the correct formula (search for "variance single pass algorithm" or "running variance") or you first calculate the mean and then set up a second loop to calculate the variance (for this case your formula is correct).
Note that the single pass algorithm for variance is numerically not as stable as using two loops, so if you can afford it memory and performance-wise you should prefer the algorithm using two passes.
PS: there are other issues with your code, but I concentrated on your main question.
推荐阅读
- java - 如何禁用 JavaFX 中的最大化选项?
- scala - Spark Scala 中的动态映射语句
- java - FOP 2.3:fo:external-graphic 的问题
- angular - 未调用 canDeActivate 函数
- html - 为什么 YouTube Player API 显示“此视频不可用”。播放某些视频时?
- c++ - 可以在光线追踪器上查看我的汽车模型
- php - Laravel 5.4:VerifyCsrfToken.php 第 68 行中的 TokenMismatchException
- visual-studio-code - Visual Studio Code - 在搜索结果中包含上下文
- visual-studio-code - Visual Studio Code:使用多个任务运行 preLaunchTask
- javascript - Javascript循环遍历变量