C ++类和函数输出问题

时间:2014-03-30 23:50:37

标签: c++ function class cout

我的程序输出有问题。它继续吐出12345。

以下是详细信息: 它分为三个文件:program8.cpp(运行测试的部分),myRandom.cpp(类的实现)和myRandom.h(类的规范)。

myRandom.h:

#ifndef MYRANDOM_H_
#define MYRANDOM_H_
class myRandom
{
    public:
            myRandom();                         //Constructor
            ~myRandom();                        //Destructor
            void seed(unsigned long theSeed);   //Mutator for current
            unsigned long next();               //Mutator or Accessor for current
            int randInt(int start, int end);    //Scales result to a range
            double randNormal();                //Future expansion
    private:
            unsigned long current;                      //Current random #
            static const unsigned long a = 1103515245;  //Multiplier for LGC
            static const unsigned long c = 12345;       //Increment for LGC
            static const unsigned long m = 2147483648;  //Modulus for LGC
};
#endif /* MYRANDOM_H_ */

myRandom.cpp:

#include <iostream>
#include <cstdlib>
#include "myRandom.h"

using namespace std;

myRandom::myRandom()                                        //Constructor
{
    current = 0;
}

myRandom::~myRandom()                                       //Destructor
{
}

void myRandom::seed(unsigned long theSeed)                  //Mutator for current
{
    if (theSeed < 0 || theSeed > m-1)
    {
        // ERROR
        return;
    }
    else
        current = theSeed;
}

unsigned long myRandom::next()                              //Mutator or Accessor for current
{
    if (current < 0)
    {
        cout << "Error: cannot set seed to a negative number" << endl;
        return 0;

    }
    else
    {
        current = (m*current+c)%m;                              //Formula
        return current;
    }
}

int myRandom::randInt(int start, int end)                   //Scales result to a range
{
    if (start >= end)
    {
        cout << "Error: cannot set start greater than or equal to end" << endl;
        return 0;
    }
    else
    {
        return ((this->next() % (end - start)) + start);
    }

}

double myRandom::randNormal()                               //Future expansion
{
    cout << "Warning: randNormal not implemented" << endl;
    return 0;
}

program8.cpp:

#include <iostream>
#include <cstdlib>
#include "myRandom.h"

using namespace std;

int main()
{
    myRandom theRand;
    unsigned long theSeed;

    cout << "Verify that the sequence generated by next() is the same on each run" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << theRand.next() << endl;
    }

    cout << "Verify that you can set the seed to 0 and 1" << endl;
    theSeed = 0;
    cout << theRand.next() << endl;
    theSeed = 1;
    cout << theRand.next() << endl;

    cout << "Verify that attempting to set the seed to -1 generates an error" << endl;
    theSeed = -1;
    cout << theRand.next() << endl;

    cout << "Verify that you can set the seed to m-2 and m-1" << endl;
    theSeed = 2147483648-2;
    cout << theRand.next() << endl;
    theSeed = 2147483648-1;
    cout << theRand.next() << endl;

    cout << "Verify that attempting to set the seed to m generates and error" << endl;
    theSeed = 2147483648;
    cout << theRand.next() << endl;

    cout << "Verify that next() produces a sequence predicted by hand/calc for the chosen seed" << endl;
    cout << "Please enter a seed: ";
    cin >> theSeed;
    cout << theRand.next() << endl;

    cout << "Verify that using start == end generates and error. Set both to 10." << endl;
    theRand.randInt(10,10);
    cout << theRand.next() << endl;

    cout << "Verify that using start > end generates and error. Set start to 10 and end to 5." << endl;
    theRand.randInt(10,5);
    cout << theRand.next() << endl;

    theRand.seed(theSeed);
    cout << "Testing randInt for start=0 end=1,000" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << theRand.randInt(0 , 1000) << endl;
    }

    return 0;
}

我认为问题在于next()函数,因为这是在program8.cpp cout语句中所有那些时间调用的。我可以理解一次获得12345,但是一旦该函数连续运行就应该更新。如果这是一个愚蠢的问题,我道歉。感谢您的时间和耐心。

1 个答案:

答案 0 :(得分:1)

您的问题不是特定于代码的问题 - 它与数学相关:

current = (m*current+c)%m;

始终返回c c < m的值,否则(或更一般地)返回c % m。为什么?从这个定理:

(m*n + a)%m = a

示例:

m = 10
n = 3
a = 7
(10*3 + 7)%10 = 7

请参阅此内容以获取更多信息:

http://en.wikipedia.org/wiki/Modulo_operation