擬似乱数列を生成できるクラスを実装しようとしています。擬似乱数の整数
コンストラクタには、初期シード、乗数、増分、および係数をパラメータとして設定する必要があります。 1つのメンバ関数は、シードを変更できるようにする必要があります。一つの関数は、擬似乱数列の次の数を生成して返さなければなりません。
私の結果は誤りです。私は間違って何をしているのか、そして正しい実装が何をすべきか。擬似ランダムシーケンスのための
ヘッダファイル:
#include<iostream>
using namespace std;
class pRandInt
{
public:
pRandInt();
//Default constructor with parameters
pRandInt(int, int, int, int);
//intial number in pseudorandom sequence
//permits the seed to be changed
void setFirstNum(int);
//generate the next number in the pseudorandom sequence
int getNextNum();
private:
int newSeed;
int newMulti;
int newIncr;
int newMod;
};
擬似ランダムシーケンスの実装ファイル:
#include "pRandInt.h"
pRandInt::pRandInt()
{
int newSeed = 0;
const int newMulti = 40;
const int newIncr = 725;
const int newMod = 729;
}
pRandInt::pRandInt(int seed, int multi, int incr, int mod)
{
newSeed = seed;
newMulti = multi;
newIncr = incr;
newMod = mod;
}
void pRandInt::setFirstNum(int seed)
{
newSeed = seed;
}
int pRandInt::getNextNum()
{
return (newMulti * newSeed + newIncr) % newMod;
}
擬似ランダム系列の主なテストファイル:
#include <iostream>
#include "pRandInt.h"
using namespace std;
int main()
{
int seed = 0;
pRandInt num;
num.setFirstNum(seed);
cout << "The first number in your sequence is: ";
cin >> seed;
cout << "The other numbers in your sequence are: ";
cout << num.getNextNum() << endl;
system("pause");
return 0;
}
特定の入力については、どのような結果が期待されましたか、実際に得られた結果は何ですか?デバッガでコードをステップ実行しようとしましたか? "正しい実装"に関しては、[標準のC++ PRNG機能](http://en.cppreference.com/w/cpp/numeric/random)を使用し、独自の実装を試みることはありません。 –