가능한 중복 : 내 64 비트 우분투 컴퓨터에서
Vastly different output C++ monte carlo approximation다른 출력
, 다음 코드는 예상대로 작동하고,와 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;
}
일반적으로 'long'은 32 비트 아키텍처에서는 32 비트이고 64 비트 아키텍처에서는 64 비트입니다. 아마 당신은 이걸로 잘못하고있는 걸까요? – Angew
@JoachimPileborg 그것은 비슷하지만 답변이 잘못되어 더 이상 값을 설정하는 데 인수를 사용하지 않고 있으며 문제가 여전히 존재합니다. – kqualters
그런 다음 수락 된 답변의 선택을 취소하고 대신 이전 질문을 편집하십시오. 질문을 편집하면 문제가 발생하여 '잡음'에서 손실되지 않습니다. –