以下是引用seagullFHcn在2006-5-9 21:24:00的发言:
const unsigned long maxshort=65536L;
class RandomNumber
{
private:
unsigned long randseed;
public:
RandomNumber(unsigned long s=0);
unsigned short Random(unsigned long n);
double fRandom(void);
};
RandomNumber::RandomNumber(unsigned long s)
{
if(s==0)
randseed=time(0);
else
randseed=s;
}
unsigned short RandomNumber::Random(unsigned long n)
{
randseed=multiplier*randseed+adder;
return (unsigned short)((randseed>>16)%n);
}
double RandomNumber::fRandom(void)
{
return Random(maxshort)/double(maxshort);
}
然后需要用随机数时写上
RandomNumber ***(变量名);
则***.fRandom()返回的就是一个0~1的double型随机数。
当然你要更大的自己再乘上放大倍数就可以了。
我最近在自学c++这东西,以后还请阁下多多帮忙了!