提交 5b8e382c 编辑于 作者: yilin wang's avatar yilin wang
浏览文件

Upload New File

上级 a6cc989c
#ifndef RANDOM_NUMBER_GENERATOR
#define RANDOM_NUMBER_GENERATOR
#include <time.h>
// used to generate random number from current seed and update seed
const unsigned long maxshort = 65536L;
const unsigned long multiplier = 1194211693L;
const unsigned long adder = 12345L;
class RandomNumber
{
private:
// private member containing current seed
unsigned long randSeed;
public:
// constructor. default 0 gives automatic seeding
RandomNumber(unsigned long s = 0);
// generate random integer 0 <= value <= n-1 (0到n-1内随机整数)
unsigned short Random(unsigned long n);
// generate random real number 0 <= value < 1.0 (0到1内随机实数)
double fRandom(void);
};
// seed the generator
RandomNumber::RandomNumber (unsigned long s)
{
if (s == 0)
randSeed = time(0); // Timestamp时间戳 (using system time)
else
randSeed = s; // user supplies the seed
}
// return random integer 0 <= value <= n-1 < 65536
unsigned short RandomNumber::Random (unsigned long n)
{
randSeed = multiplier * randSeed + adder;
return (unsigned short)((randSeed >> 16) % n);
}
// return (value in range 0..65535) / 65536
double RandomNumber::fRandom (void)
{
return Random(maxshort)/double(maxshort);
}
#endif // RANDOM_NUMBER_GENERATOR
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册