Поправь типы.
| Код |
#ifndef __LRANDOM_H__ #define __LRANDOM_H__
#include <math.h> #include <time.h> #include "lglobal.h" #include "lfile.h"
#define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define NTAB 32 #define NDIV (1+(IM-1)/NTAB) #define EPS 1.2e-7 #define RNMX (IM-1)
namespace lcl {
/***************************************************************************/
class LCL_EXPORT LRandom { friend LBaseFile& operator<< ( LBaseFile&, const LRandom& ); friend LBaseFile& operator>> ( LBaseFile&, LRandom& ); public: LRandom():idum(-1) {} LRandom( llong s ):idum(s) {} /**/ inline void seed( llong s ) { idum = s; }; llong rand() { int j; long k; static long iy=0; static long iv[NTAB]; if ( idum <= 0 || !iy ) { if ( -(idum) < 1 ) idum=1; else idum = -(idum); for ( j = NTAB + 7; j >= 0; j-- ) { k = (idum) / IQ; idum = IA * (idum - k * IQ) - IR * k; if ( idum < 0 ) idum += IM; if ( j < NTAB ) iv[j] = idum; } iy = iv[0]; } k = (idum) / IQ; idum = IA * (idum - k * IQ) - IR * k; if ( idum < 0 ) idum += IM; j = iy / NDIV; iy = iv[j]; iv[j] = idum; if ( iy > RNMX ) return RNMX; return iy; } protected: llong idum; };
/***************************************************************************/ LBaseFile& operator<< ( LBaseFile& f, const LRandom& o ); LBaseFile& operator>> ( LBaseFile& f, LRandom& o ); /***************************************************************************/
class LCL_EXPORT LRandomizeArray:public LRandom, public LVector<luint32> { friend LBaseFile& operator<< ( LBaseFile&, const LRandomizeArray& ); friend LBaseFile& operator>> ( LBaseFile&, LRandomizeArray& ); public: LRandomizeArray():LRandom(),LVector<luint32>() {} LRandomizeArray( luint32 l ):LRandom(),LVector<luint32>( l ) { randomize(); } /**/ void randomize() { for ( LVector<luint32>::iterator it = beg(); it != end(); it++ ) { *it = rand(); } } void clear() { LVector<luint32>::clear(); idum = 0; } };
/***************************************************************************/ LBaseFile& operator<< ( LBaseFile& f, const LRandomizeArray& o ); LBaseFile& operator>> ( LBaseFile& f, LRandomizeArray& o ); /***************************************************************************/
}; // namespace "lcl"
#endif // __LRANDOM_H__
|
| Код |
#include "lcl/lrandom.h"
namespace lcl {
/***************************************************************************/ LBaseFile& operator<< ( LBaseFile& f, const LRandom& o ) { f << o.idum; return f; }
LBaseFile& operator>> ( LBaseFile& f, LRandom& o ) { f >> o.idum; return f; }
/***************************************************************************/ LBaseFile& operator<< ( LBaseFile& f, const LRandomizeArray& o ) { f << o.size() << o.idum; for ( LRandomizeArray::const_iterator it = o.beg(); it != o.end(); it++ ) { f << *it; } return f; }
LBaseFile& operator>> ( LBaseFile& f, LRandomizeArray& o ) { luint32 s = 0; f >> s >> o.idum; L_ASSERT( o.resize( s ), NOT_FREE_MEMORY ); for ( luint32 idx = 0; idx < s; idx++ ) { f >> o[idx]; } return f; } /***************************************************************************/
}; // namespace "lcl"
|
|