-2
#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;  
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
raneem
  • 1
  • 3

2 Answers2

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