2012-11-27 21 views
2

使用随机数生成器来做Pi的C++近似,输出的结果与运行Ubuntu的AMD 64计算机上的预期完全一样,但是在我的学校机器上,我实现的第二个算法已经失效,并会喜欢一些见解为什么。代码如下:完全不同的输出C++蒙特卡罗近似

#ifndef RANDOMNUMBER_H_ 
#define RANDOMNUMBER_H_ 

class RandomNumber { 
public: 
RandomNumber() { 
    x = time(NULL); 
    m = pow(2, 19); //some constant value 
    M = 65915 * 7915; //multiply of some simple numbers p and q 
    method = 1; 
} 
RandomNumber(int seed) { 
    x = ((seed > 0) ? seed : time(NULL)); 
    m = pow(2, 19); //some constant value 
    method = 1; //method number 
    M = 6543 * 7915; //multiply of some simple numbers p and q 
} 
void setSeed(long int seed) { 
    x = seed; //set start value 
} 

void chooseMethod(int method) { 
    this->method = ((method > 0 && method <= 2) ? method : 1); //choose one of  two method 
} 

long int linearCongruential() { //first generator, that uses linear congruential method 
    long int c = 0; // some constant 
    long int a = 69069; //some constant 
    x = (a * x + c) % m; //solution next value 
    return x; 
} 

long int BBS() { //algorithm Blum - Blum - Shub 
    x = (long int) (pow(x, 2)) % M; 
    return x; 
} 
double nextPoint() { //return random number in range (-1;1) 
    double point; 
    if (method == 1) //use first method 
     point = linearCongruential()/double(m); 
    else 
     point = BBS()/double(M); 
    return point; 
} 
private: 
long int x; //current value 
long int m; // some range for first method 
long int M; //some range for second method 
int method; //method number 
}; 

#endif /* RANDOMNUMBER_H_ */ 

和测试类:

#include <iostream> 
#include <stdlib.h> 
#include <math.h> 
#include <iomanip> 
#include "RandomNumber.h" 
using namespace std; 

int main(int argc, char* argv[]) { 
cout.setf(ios::fixed); 
cout.precision(6); 
RandomNumber random; 
random.setSeed(argc); 
srand((unsigned) time(NULL)); 
cout << "---------------------------------" << endl; 
cout << " Monte Carlo Pi Approximation" << endl; 
cout << "---------------------------------" << endl; 
cout << " Enter number of points: "; 
long int k1; 
cin >> k1; 
cout << "Select generator number: "; 
int method; 
cin >> method; 
random.chooseMethod(method); 
cout << "---------------------------------" << endl; 
long int k2 = 0; 
double sumX = 0; 
double sumY = 0; 
for (long int i = 0; i < k1; i++) { 
    double x = pow(-1, int(random.nextPoint() * 10) % 2) 
      * random.nextPoint(); 
    double y = pow(-1, int(random.nextPoint() * 10) % 2) 
      * random.nextPoint(); 
    sumX += x; 
    sumY += y; 
    if ((pow(x, 2) + pow(y, 2)) <= 1) 
     k2++; 

} 
double pi = 4 * (double(k2)/k1); 
cout << "M(X) = " << setw(10) << sumX/k1 << endl; //mathematical expectation of x 
cout << "M(Y) = " << setw(10) << sumY/k1 << endl; //mathematical expectation of y 
cout << endl << "Pi = " << pi << endl << endl; //approximate Pi 

return 0; 
} 

第二种方法始终返回4.000我的实验室的机器上,但返回我的个人计算机上的一个相当接近。

+0

听起来像整数长度或其他一些差异。或者你也许已经进入了整数溢出等“未定义”的领域。 –

+0

你正在使用什么编译器? – piokuc

+0

什么操作系统和编译器是你的实验室机器? –

回答

4

首先,你使用的BBS生成器将始终返回1

由于您的程序没有参数,推测其argc将是1。你通过argc作为种子(为什么?),所以x的初始值是1

BBS()具有以下逻辑:

x = (long int) (pow(x, 2)) % M; 

显然,1平方模M1,所以x永远不会改变。

当您使用这样的生成器运行模拟程序时,程序将始终输出4

P.S.维基百科有以下说关于初始值x0Blum Blum Shub

种子x0应该是一个整数,它的互质数,以M(即pq不是的x0因素),而不是10

+1

也许他们用参数运行它?我敢打赌,他们打算通过'atoi(argv [1])'。 – Xymostech

+0

@Xymostech:这就是我认为的(两点)。 – NPE

+0

我的意思是如果传递参数,然后调用setSeed函数,否则使用系统时间。工作,但感谢您的帮助。 – kqualters