C++ 为什么vc++;为什么会出现这种统计模式?
我正在运行以下程序:C++ 为什么vc++;为什么会出现这种统计模式?,c++,visual-c++,random,statistics,C++,Visual C++,Random,Statistics,我正在运行以下程序: #include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <chrono> using namespace std; const int N = 200; // Number of tests. const int M = 2000000; // Number of pseudo
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <chrono>
using namespace std;
const int N = 200; // Number of tests.
const int M = 2000000; // Number of pseudo-random values generated per test.
const int VALS = 2; // Number of possible values (values from 0 to VALS-1).
const int ESP = M / VALS; // Expected number of appearances of each value per test.
int main() {
for (int i = 0; i < N; ++i) {
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
srand(seed);
vector<int> hist(VALS, 0);
for (int j = 0; j < M; ++j) ++hist[rand() % VALS];
int Y = 0;
for (int j = 0; j < VALS; ++j) Y += abs(hist[j] - ESP);
cout << Y << endl;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int N=200;//测试次数。
常数int M=2000000;//每次测试生成的伪随机值的数量。
常数int VALS=2;//可能值的数量(从0到VAL-1的值)。
常量int ESP=M/VALS;//每次测试每个值的预期出现次数。
int main(){
对于(int i=0;i cout该程序将生成分布不均匀的随机数(不均匀、独立)
兰德的功能是出了名的差
使用余数运算符%
将数字带入范围有效地丢弃除低阶位以外的所有位
每次通过循环时,RNG都会重新播种
[编辑]我刚刚注意到const int ESP=M/VALS;
。您想要一个浮点数
尝试下面的代码并报告。使用新的<;random>有点乏味。许多人编写一些小型库代码以简化其使用
#include <iostream>
#include <vector>
#include <cmath>
#include <random>
#include <chrono>
using namespace std;
const int N = 200; // Number of tests.
const int M = 2000000; // Number of pseudo-random values generated per test.
const int VALS = 2; // Number of possible values (values from 0 to VALS-1).
const double ESP = (1.0*M)/VALS; // Expected number of appearances of each value per test.
static std::default_random_engine engine;
static void seed() {
std::random_device rd;
engine.seed(rd());
}
static int rand_int(int lo, int hi) {
std::uniform_int_distribution<int> dist (lo, hi - 1);
return dist(engine);
}
int main() {
seed();
for (int i = 0; i < N; ++i) {
vector<int> hist(VALS, 0);
for (int j = 0; j < M; ++j) ++hist[rand_int(0, VALS)];
int Y = 0;
for (int j = 0; j < VALS; ++j) Y += abs(hist[j] - ESP);
cout << Y << endl;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
const int N=200;//测试次数。
const int M=2000000;//每次测试生成的伪随机值数。
const int VALS=2;//可能值的数目(从0到VALS-1的值)。
const double ESP=(1.0*M)/VALS;//每个测试值的预期出现次数。
静态标准::默认的随机引擎;
静态空种子(){
std::随机_装置rd;
种子(rd());
}
静态整数和整数(整数低,整数高){
标准:统一内部分布区(lo,hi-1);
返回距离(发动机);
}
int main(){
种子();
对于(int i=0;i 好吧,但我的问题呢。rand()
on。它是一个线性同余生成器(非常简单,但不是非常随机),只输出16位数字。在std::rand
上运行此测试套件(MSVC版本)然后发现你没有一个真正的随机数生成器。所以这是一个垃圾的例子。你还和我们在一起吗?谢谢你的评论和代码。统一分布缺少它的数据类型声明。你的程序生成的数据遵循某种半正态分布(如预期的那样)。我的问题仍然没有答案。即使rand()的缺陷很重要,了解1.为什么它们在这种情况下很重要以及它们如何影响数据和2.为什么输出数据取决于编译器。我确实回答了这个问题,“你对此有何想法,解释是什么?”解释分为三个部分。至于为什么不同的编译器会给出不同的结果,可以归结为rand
的不同实现。VC++实现可能比大多数更糟糕。为什么rand()中的缺陷问题:垃圾输入,垃圾输出。一个深入的解释太复杂了,无法在这里深入讨论。看这个:uniform\u int\u distribution
的默认类型是int
。拿礼物马来说。顺便说一句,Marsaglia的论文更多地证明了关于软件工程的一切都是在1968年发现的。