70,023
社区成员




#ifndef RANDOM_NUMBER
#define RANDOM_NUMBER
const unsigned long maxshort = 65536L;
const unsigned long multiplier = 1194211693L;
const unsigned long adder = 12345L;
class RandomNumber
{
private:
unsigned long randSeed;
public:
RandomNumber(unsigned long s = 0);
unsigned short Random(unsigned long n);
double fRandom(void);
};
#endif
#include "RandomNumber.h"
#include <time.h>
RandomNumber::RandomNumber(unsigned long s)
{
time_t t;
t = time(NULL);
tm *p = localtime(&t);
if(s == 0)
randSeed = (p->tm_hour * 60 + p->tm_min) * 60 + p->tm_sec;
else
randSeed = s;
}
unsigned short RandomNumber::Random(unsigned long n)
{
randSeed = multiplier * randSeed + adder;
return (unsigned short)((randSeed >> 16) % n);
}
double RandomNumber::fRandom()
{
return Random(maxshort)/double(maxshort);
}