#include <iostream>
#include <map>
using namespace std ;
#include <time.h>
#include <math.h>
//C语言的库函数rand有以下两个可改进之处
//范围太小,才3万多,改成40亿多
//需要srand,如果不srand 默认只是1。CRand默认值是clock()。注意:Windows下GetTickCount更精确
#define ULONG unsigned long
//接口
class IRand
{
public:
//不实现SetSeed防止多次SetSeed引起种子值相同。构造函数中初始化种子
virtual ULONG Rand()=0;
};
class CRand : public IRand
{
public:
CRand(ULONG s1 = clock(),ULONG s2 = time(NULL) )
{
m_iSeed1 = s1 ;
m_iSeed2 = s2 ;
}
ULONG Rand()
{
ULONG b;
b = m_iSeed1 ^ (m_iSeed1 >> 2) ^ (m_iSeed1 >> 6) ^ (m_iSeed1 >> 7);
m_iSeed1 = (m_iSeed1 >> 1) | (~b << 31);
b = (m_iSeed2 << 1) ^ (m_iSeed2 << 2) ^ (m_iSeed1 << 3) ^ (m_iSeed2 << 4);
m_iSeed2 = (m_iSeed2 << 1) | (~b >> 31);
return m_iSeed1 ^ m_iSeed2;
}
protected:
ULONG m_iSeed1;
ULONG m_iSeed2;
};
void main()
{
const int n = 100;
CRand r ;
for( int i = 0 ; i < n ; i++ )
{
cout << r.Rand() << endl ;
}
}