Vastly different output C++ monte carlo approximation

前端 未结 1 937
别跟我提以往
别跟我提以往 2021-01-21 20:29

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 al

1条回答
  •  粉色の甜心
    2021-01-21 21:21

    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 讨论(0)
提交回复
热议问题