Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:01:39+00:00 2026-06-15T07:01:39+00:00

doing a C++ approximation of Pi using a random number generator, output works exactly

  • 0

doing a C++ approximation of Pi using a random number generator, output works exactly as expected on my AMD 64 machine running Ubuntu, however on my school machine the second algorithm I’ve implemented is broken, and would love some insight as to why. Code is as follows:

#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_ */

and test class:

#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;
}

The second method returns 4.000 consistently on my lab machine, yet returns a rather close approximation on my personal machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:01:42+00:00Added an answer on June 15, 2026 at 7:01 am

    For one thing, the BBS generator as you’re using it will always return 1.

    Since your program takes no arguments, presumably its argc will be 1. You pass argc as the seed (why?), so the initial value of x is 1.

    BBS() has the following logic:

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

    Clearly, 1 squared modulo M gives 1, so x never changes.

    When you run the simulation with such a generator, your program will always output 4.

    P.S. Wikipedia has the following to say about the initial value x0 for Blum Blum Shub:

    The seed x0 should be an integer that’s co-prime to M (i.e. p and q are not factors of x0) and not 1 or 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing an ajax get request works as expected using the following code: $.ajax({ type:
I am using an MVP architecture (or at least an approximation of one) and
Doing this works in IE7: <a href= target=_blank>Link</a> But in IE8 it open a
Recently thanks to rails' popularity, many people start using activerecord as model. however, before
I am doing a Monte Carlo experiment to calculate an approximation of PI. From
Doing some inline assembly in clang (basically guessing my way through by using various
I have been doing this using an svd computation [U, S, V] = svd(A)
Doing cross platform development with 64bit. Using gcc/linux and msvc9/server 2008. Just recently deployed
doing some C++/CLI coding again, and running into issues. Working in VS2008 if it
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.