我需要建立一个条形图来说明由线性同余方法确定的伪随机数的分布
Xn+1 = (a * Xn + c) mod m
U = X/m
在区间 [0,1]
例如:间隔频率
[0;0,1] 0,05
[0,1;0,2] 0,15
[0,2;0,3] 0,1
[0,3;0,4] 0,12
[0,4;0,5] 0,1
[0,5;0,6] 0,15
[0,6;0,7] 0,05
[0,7;0,8] 0,08
[0,8;0,9] 0,16
[0,9;1,0] 0,4
我写过这样的程序
lcg.h:
class LCG {
public:
LCG();
~LCG();
void setSeed(long);
float getNextRand();
void countFrequency();
void printFrequency();
private:
vector<int>frequencies;
long seed;
static const long a = 33;
static const long c = 61;
static const long m = 437;
};
lcg.cpp:
void LCG::setSeed(long newSeed)
{
seed = newSeed;
}
LCG::LCG() {
setSeed(1);
}
LCG::~LCG() { }
float LCG::getNextRand() {
seed = (seed * a + c) % m;
return (float)seed / (float)m;
}
void LCG::countFrequency()
{
for (int i = 0; i < 10; ++i)
frequencies[i] = 0;
for (int i = 0; i < m; ++i)
{
float u = getNextRand();
int r = ceil(u * 10.0);
frequencies[r] = frequencies[r] + 1;
}
}
void LCG::printFrequency()
{
for (int i = 0; i < 10; ++i)
{
const float rangeMin = (float)i / 10.0;
const float rangeMax = (float)(i + 1) / 10.0;
cout << "[" << rangeMin << ";" << rangeMax << "]"
<< " | " << frequencies[i] << endl;
}
}
主.cpp:
int main()
{
LCG l;
l.countFrequency();
l.printFrequency();
}
它可以正确编译和 lint,但不想运行。我不知道我的程序有什么问题。函数 countFrequency 和 printFrequency 有问题。但我不知道是什么。也许你知道?