#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
class mRND
{
public:
void seed()
{
srand(time(0));
_seed = rand();
}
protected:
mRND() : _seed(), _a(), _c(), _m(2147483648)
{
}
int rnd()
{
return (_seed = (_a * _seed + _c) % _m);
}
int _a, _c;
unsigned int _m, _seed;
};
int main() {
mRND r;
for(int i=0;i<100;i++)
cout<< r.rnd()<<endl;
return 0;
}
Asked
Active
Viewed 147 times
-2

Thomas Sablik
- 16,127
- 7
- 34
- 62

raneem
- 1
- 3
-
You never give _seed, _a or _c any values, so it just keeps using 0 and 0*0+0 = 0. – Delta_G Aug 08 '20 at 23:28
2 Answers
4
Your problem is here:
return (_seed = (_a * _seed + _c) % _m);
_a
is 0 so the returned value is 0 and it never changes (since _a
is always 0).

Object object
- 1,939
- 10
- 19
-8
Try setting time as "NULL" rather than zero. Zero, in this case, would likely mean midnight on January 1st, 1970. If this is the case, you would keep getting the random seed that occurs with that time.

mpekim
- 29
- 1
- 1
- 6