0

可能的重复:
输出 C++ 蒙特卡罗近似有很大不同

在我的 64 位 ubuntu 计算机上,以下代码按预期工作,并使用两种算法返回 pi 的近似值。但是,在我必须演示代码的实验室机器上,一台 32 位 rhel 3 机器上,第二个算法总是返回 4,我不知道为什么。任何见解将不胜感激。

/*
 * RandomNumber.h
 *
 *  
 *      
 */

#ifndef RANDOMNUMBER_H_
#define RANDOMNUMBER_H_

class RandomNumber {
public:
RandomNumber() {
    x = time(NULL);
    m = pow(2, 31); //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, 31); //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() {
cout.setf(ios::fixed);
cout.precision(6);
RandomNumber random;
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

3 回答 3

1

问题是pow返回 a double,它在低端失去精度。转换为long intfor%运算符始终返回相同的结果,因此您的 RNG 输出常量 -60614748。

x = time(0)                 1354284781
pow(x, 2)                  1.83409e+18   0x1.973fdc9dc7787p+60
(long int) pow(x, 2)       -2147483648    0x80000000
(long int) pow(x, 2) % M     -60614748

解决方法是更改x = (long int) (pow(x, 2)) % M;​​为x = x * x % M,在 中执行所有算术运算long int。请注意,严格来说这仍然是不正确的,因为有符号溢出是未定义的;更正确的是使用unsigned long.

于 2012-11-30T14:18:45.033 回答
0

截断到longinBBS()会导致生成相同的“随机”数。

PS。该pow函数的返回是一个数字,它太大而无法以您的机器long类型表示。进行转换时会long导致未定义的行为。未定义行为的一个特殊影响可能是转换结果是0x80000000或者0x7fffffff你最终得到一个相同数字的序列。

于 2012-11-30T14:04:17.007 回答
0
x = time(0)                 1354284781
pow(x, 2)                  1.83409e+18   0x1.973fdc9dc7787p+60

一个 32 位 int 的值最大为 2^31-1,x^2 的值大于该值。

于 2012-11-30T14:39:57.850 回答